|
Posted by Tim Van Wassenhove on 01/13/06 07:47
On 2006-01-13, Tim Van Wassenhove <timvw@users.sourceforge.net> wrote:
> On 2006-01-13, Chung Leong <chernyshevsky@hotmail.com> wrote:
>> Here's a little brain teaser distilled from a bug that took me a rather
>> long time to figure out. The two functions in the example below behave
>> differently. The difference is easy to spot, of ocurse. The challenge
>> is correctly explaining why this is so. Why does the second function
>> seemingly corrupt the cloned copy of an object?
>
> My guess is the following:
> When php initializes a new object (copy/clone) it will notice that in the second
> method the reference count to $obj->attributes is 2. Therefore it will assign
> new memory for the copied/cloned instance. This explains why we see 0 instead of
> 14.
Actually, the new memory is not assigned when the constructor is called, but
when the $obj->attributes['Length'] is set to 0 in the second method (the
copy-on-write behaviour of php).
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be>
[Back to original message]
|