SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing class objects from inside other classes

    Hi,

    Sorry if this is a noob question, I'm just learning OOP!

    Say I have this code:

    PHP Code:
    include (CLASS_FOLDER.'a.php');
    include (
    CLASS_FOLDER.'b.php');

    $a = new ClassA (...);
    $b = new ClassB (...); 
    Now if ClassA is my database class and I need to use the database functions inside every other class (and function), whats the best way to do this?

    Is it using extend:
    class ClassB extends ClassA {...}

    or to make a new object of ClassA inside ClassB (so there would be an object of ClassA inside every other class used)?

    And inside (non-class) functions, can someone tell me the best way to do this - using global $a, or some other way?

    Thanks for any help,
    AlienDev
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I prefer to store a single database connection/object in a Registry:
    http://www.patternsforphp.com/wiki/Registry
    Any class that needs it would grab an instance of the existing Registry, then grab the instance of the existing database connection/object.

    Others may prefer to pass in the database object as a parameter of the constructor or whatever method needs it.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create the database object, and pass it to the functions or objects which need to use it. You can pass dependencies to an object, in the constructor.

  4. #4
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So..

    PHP Code:
    $b = new ClassB ($a); 
    Is the best way?
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's one way, but I would do it like this (maybe because I only use OOP in C#):
    PHP Code:
    Class B{
        var 
    $a;
        function 
    assignClass($class){
            
    $this->$class;
        }

    and to call it:
    PHP Code:
    $a = new ClassA;
    $b = new ClassB;
    $b->assignClass($a); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •