note 31228 deleted from language.references by sniper

From: Date: Tue, 30 Sep 2003 01:18:55 +0000
Subject: note 31228 deleted from language.references by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57317@lists.php.net to get a copy of this message
Note Submitter: thomas@zuschneid.de ---- I used objects inside objects, like this: class ItemClass { function PrintContainerValue() { print $this->container->value; } } class ContainerClass { var $value=5; function ContainerClass(&$item) { $item->container = &$this; $this->item = $item; } } $aContainer = new ContainerClass(new ItemClass); // 1 $aContainer->value = 10; // 2 $aContainer->item->PrintContainerValue(); It was a long quest to figure out why this prints 5, not 10. But the solution is quite simple: The "new" in line 1 creates a ContainerClass object with value 5 and an item referencing to this instance. $aContainer gets only a copy whose "value" is changed in line 2, but item->container still references the first instance with unchanged "value". Replacing new with &new solved the problem.

« previous php.notes (#57317) next »