SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Feb 2004
    Location
    france
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    objects communicating with each other

    hi there

    for as long as i've been coding in an OOP fashion, the question of how the objects should communicate with each others came back to me. for exemple, i've got a dba class, called db, which must be used in my sessions handling class.
    the obvious way is to make the $db object global, but i think this is not the good way (harry told us global is evil heh)
    i see two other ways:

    - passing the $db object to the session constructor, like this:

    PHP Code:

    class sessions {

        function 
    sessions(&$db) {
        
            
    $this->db = & $db;
            
        }
        
        function 
    foo() {
        
            
    // then using the object like this
            
            
    $this->db->query();
            
        }
        

    it works well, but still heavy to use as far as i'm concerned.

    i then developped my own system, based on a kernel that loads classes and save the objects as attributes, passing a reference of itself to each object.

    PHP Code:

    class kernel {

        function 
    loadLib($lib) {
        
            
    $this->$lib = new $lib($kernel);
            
        }
        
    }

    class 
    db {

        function 
    db(&$kernel) {
            
            
    $this->kernel = & $kernel;
            
        }
        
    }

    class 
    sessions {

        function 
    sessions (&$kernel) {
        
            
    $this->kernel = & $kernel;
            
        }
        
        function 
    foo() {
        
            
    // now we use it like this
            
            
    $this->kernel->db();
            
        }
        
    }

    $kernel = new kernel();
    $kernel->loadLib('db');
    $kernel->loadLib('sessions');

    $kernel->sessions->foo(); 
    it works well too, but i don't think this is a really good way...

    am i missing something important in OOP philosophy ?
    we are the knigts who say nee !

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by actarus
    the obvious way is to make the $db object global, but i think this is not the good way (harry told us global is evil heh)
    [...]
    am i missing something important in OOP philosophy ?
    what about the famous singleton-pattern ?

    PHP Code:
    class MyClass
    {
    function 
    MyClass()
        {
        }

    function &
    singleton()
        {
        static 
    $i NULL;

        if (
    $i === NULL)
            {
            
    $i = new Object();
            }

        return 
    $i;
        }
    }

    $obj =& MyClass::singleton();
    $obj2 =& MyClass::singleton(); 
    these two are the same instances and you may fetch the instance
    from any scope, e.g. from a class-method, function, etc...

    hth,
    kai

  3. #3
    SitePoint Member
    Join Date
    Feb 2004
    Location
    france
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i always wondered the usefulness of the singleton pattern... thanks for this explanation
    we are the knigts who say nee !

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your welcome

    for a really in-depth-explanation see harry fücks's article over at phppatterns.com

  5. #5
    SitePoint Enthusiast DaveCheney's Avatar
    Join Date
    Dec 2003
    Location
    Melbourne, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kaimeder
    what about the famous singleton-pattern ?

    PHP Code:
    class MyClass
    {
    function 
    MyClass()
        {
        }

    function &
    singleton()
        {
        static 
    $i NULL;

        if (
    $i === NULL)
            {
            
    $i = new Object();
            }

        return 
    $i;
        }
    }

    $obj =& MyClass::singleton();
    $obj2 =& MyClass::singleton(); 
    these two are the same instances and you may fetch the instance
    from any scope, e.g. from a class-method, function, etc...

    hth,
    kai
    In PHP4 how would you simulate the static $i ? Maybe an entry in $GLOBALS ?

    Cheers

    Dave

  6. #6
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Netherlands
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveCheney
    In PHP4 how would you simulate the static $i ? Maybe an entry in $GLOBALS ?

    Cheers

    Dave
    The code you've quoted is valid and pure PHP4, php5 will not work or complain because of the "&" in "function &singleton()".

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought I remembered PHP4 having a problem with a static reference. Try making the instance variable a static array, and store the reference in $i[0] to get around this.

    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.


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
  •