I have some code that appears to behave differently between PHP 4 and PHP 5. This code below:
class CFoo { var $arr; function CFoo() { $this->arr = array(); } function AddToArray($i) { $this->arr[] = $i; } function DoStuffOnFoo() { for ($i = 0; $i < 10; ++$i) { $foo2 = new CFoo(); $foo2 = $this; // I expect this to copy, therefore // resetting back to the original $this $foo2->AddToArray($i); echo "Foo2:\n"; print_r($foo2); echo "This:\n"; print_r($this); } } } $foo1 = new CFoo(); $foo1->DoStuffOnFoo(); Previously, in PHP 4, the assignment of $foo2 above would reset $foo2 back to the value that $this was originally set at. In this case, I would expect it to be set to a CFoo with an empty $arr member. However, the assignment of $foo2 to $this is acting as an assignment by reference. Foo2 is acting as an alias to this. Therefore when I call "AddToArray" on foo2, $this's $arr is also being appended to. So when I go to reassign foo2 back to this, instead of getting the initial value of this, I get essentially a self assignment.
Has this behavior changed in PHP 5? What can I do to force foo2 to make a copy of this?