note 31228 added to language.references

From: Date: Mon, 14 Apr 2003 16:27:07 +0000
Subject: note 31228 added to language.references
Groups: php.notes 
Request: Send a blank email to php-notes+get-46604@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/language.references.php http://master.php.net/manage/user-notes.php?action=edit+31228 http://master.php.net/manage/user-notes.php?action=delete+31228 http://master.php.net/manage/user-notes.php?action=reject+31228

« previous php.notes (#46604) next »