View Single Post
Old 12-01-2007, 02:15 AM   #11 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

There's a very simple solution.

Change: (in the class2 constructor)
PHP Code:
$this->class1 $class1
To:
PHP Code:
$this->class1 =& $class1
Why does this work? It's all about the way that PHP4 handles passing around references to objects. The &$class1 argument for the constructor ensured that the argument was brought in by reference, but when it was being assigned to the class1 property the assignment was by value -- a copy of the object was made and assigned to the property.

When we changed the data property, it was only changing it in the copy and not the original. Using =& the variable is assigned by reference as we intended. Hopefully you can see now why this behaviour was changed in PHP5 to assign by reference by default. :)
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
MartynMJ (12-01-2007)