SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Boston, MA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Give 2 classes access to same object?

    I might already know the answer to this, but I'd like some confirmation...

    How do I give two classes access to the same object of a third class? For instance, Class B creates an object of Class A, how can I make Class C have access to that same object without using extension or implementation? Would something like the following work?:
    PHP Code:
    class {
      function 
    {
        
    $this->obj =& new A();
      }
    }

    class 
    {
      function 
    {
         
    $this->=& new B();
         
    $this->obj $this->b->obj;
      }

    Basically, when I update B->obj I want C->obj to be that updated object automatically. Hope that makes sense.

  2. #2
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks fine to me, other than:
    PHP Code:
    class {

      var 
    $b;
      var 
    $obj;

      function 

         
    $this->=& new B(); 
         
    $this->obj $this->b->obj
      } 

    Hope that helps?

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Boston, MA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Forbes
    Looks fine to me, other than:
    PHP Code:
    class {

      var 
    $b;
      var 
    $obj;

      function 

         
    $this->=& new B(); 
         
    $this->obj $this->b->obj
      } 

    Hope that helps?
    Heh, I left off the declarations for space reasons.

    So my understanding is, if a class var of B->obj is changed AFTER C->obj is instantiated, when C->obj accesses it, it will be the same value. I rarely trust my initial thoughts on these matters!

    Thanks!

  4. #4
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I rarely trust my own .. or even yours for that matter!

    But there's always a cast-iron way of telling whether you're right or wrong -- try running the code and put in the some values and test things...

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $this->obj = &$this->b->obj; will probably do

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Boston, MA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn, doesn't work

    PHP Code:
    class {
        
        var 
    $z;
        
        function 
    A() {
            
    $this->0;
        }
        
        function 
    add() {
            
    $this->z++;
        }
        
        function 
    toString() {
            echo 
    $this->z;
        }
    }

    class 
    {
        var 
    $obj;
        
        function 
    B() {
            
    $this->obj =& new A();
        }
    }

    class 
    {
        var 
    $obj;
        var 
    $b;
        
        function 
    C() {
            
    $this->=& new B();
            
    $this->obj =& $this->b->obj;
        }
    }

    $b =& new B();
    $c =& new C();

    $b->obj->toString();
    $b->obj->add();
    $c->obj->toString();
    $b->obj->toString();

    // Output:  001
    // Should:  011 
    However, this appears to...
    PHP Code:
    class {
        
        var 
    $z;
        
        function 
    A() {
            
    $this->0;
        }
        
        function 
    add() {
            
    $this->z++;
        }
        
        function 
    toString() {
            echo 
    $this->z;
        }
    }

    class 
    {
        var 
    $obj;
        
        function 
    B() {
            
    $this->obj =& new A();
        }
    }

    class 
    {
        var 
    $obj;
        var 
    $b;
        
        function 
    C(&$obj) {
            
    $this->obj =& $obj;
        }
    }

    $b =& new B();
    $c =& new C($b->obj);

    $b->obj->toString();
    $b->obj->add();
    $c->obj->toString();
    $b->obj->toString();

    // Output:  011
    // Should:  011 
    (Yes, I am aware of the sloppiness of this code, what with directly accessing member variables and having toString echo something, but it's just an example! )
    Last edited by binjured; Dec 21, 2005 at 12:01.


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
  •