Posted by Chung Leong on 01/14/06 05:55
Tim Van Wassenhove wrote:
>
> I still think it's because after this statement the container that holds the
> $obj->attributes will now have is_ref = 1 which seems to lead to a copy of this
> container when $obj is assigned to $clone ($clone = $obj). For some reason php
> doesn't seem to make a deep-copy of the array but simply makes an array with
> references to the original array elements.
That's it. You have got to the root of the problem. During cloning, PHP
does not create a copy of an object property if it is a reference. From
the manual: "Any properties that are references to other variables,
will remain references." The hard part was, of course, recognizing that
$obj->attributes is a reference. It's extremely unintuitive that the
line
$attr =& $obj->attributes;
not only makes $attr a reference, but turns $obj->attributes into a
reference too. Programmers usually don't expect side-effects on the
right side of an assignment operator.
[Back to original message]
|