Problems with Parent Child References

From: Date: Sun, 27 Jan 2002 01:46:35 +0000
Subject: Problems with Parent Child References
Groups: php.general 
Request: Send a blank email to php-general+get-82261@lists.php.net to get a copy of this message
I've designed an architecture in PHP which is dependent on a class hierarchy which has a parent class with a member variable which is a child object. That child object contains a reference back to the parent. The problem I'm running into is changes to the parent's member variables are not reflected in the child's reference to the parent. Let me use a code example to illustrate my point. Class Parent { var $sTest = "apple"; var $pChild; function Parent() { $this->pChild = new Child(); $this->pChild->SetParent($this); } } Class Child { var $pParent; function SetParent(&$pParent) { $this->pParent = &$pParent; } function PrintParentData() { echo "Parents Test data: ".$this->pParent->sTest."<Br>\n"; } } $pParent = new Parent(); $pParent->sTest = "orange"; echo "pParent->sTest = ".$pParent->sTest."<br>\n"; $pParent->pChild->PrintParentData(); // Prints: // pParent->sTest = orange // Parents Test data: apple // Instead of : // pParent->sTest = orange // Parents Test data: orange I've also tried changing the first line of the Parent constructor to : $this->pChild = & new Child(); I think I've coded this correctly as per the docs on references. I'm using PHP4.1.1. My conclusions are : 1. This is not possible in PHP due to safety mechanisms built into the language to safeguard against circular references (and infinite recursion). 2. I'm doing something wrong. Thanks to anyone willing to take a stab... Kevin Morris

« previous php.general (#82261) next »