Get parent object from object

Is it possible?

Let's say there are two objects of the same type:

$object1->object2->property = 'xxxx';

this is now done via __set (). At this point, I get into the scope of object2 (which is a property of object1). How can I access object1 from this __set function?

+3


source to share


2 answers


You can not.

object1

is not a parent, it is a container. If you want to access a function object1

from object2

, you must have a link to object1

.

Use this type of template:



class class1 
{ 
   public $child; 
   public function __construct() 
   { 
      $this->child = new class2($this);
   }
} 

class class2 
{
   private $parent;
   public function __construct(class1 $parent) 
   {
      $this->parent = $parent; 
   }
}

      

Is this what you are looking for?

+4


source


Try using parent::

from a child class.



0


source







All Articles