|
Posted by Jim Michaels on 01/15/06 11:44
"Chung Leong" <chernyshevsky@hotmail.com> wrote in message
news:1137210925.742895.267450@g44g2000cwa.googlegroups.com...
> 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.
>
This doesn't sound technically correct. an operator (such as =& ) in a
parser is generally never allowed to modify the RHS (right-hand-side). This
jives with the article he mentioned.
The correct way to read $b =& $a is $b is assigned a reference to $a.
according to the diagrams shown, $a and $b are both C pointers (to data) in
the first place.
in C (under the hood), the pointer assignment is simply b=a.
PHP's = is probably a simple C call to malloc() the LHS pointer to allocate
some memory and then memcpy() to copy the RHS data over that newly allocated
memory.
Ever wonder why a plain var like $a can be assigned NULL? because possibly
a C pointer can be assigned NULL. You learn this stuff in ANSI C classes
(which is what PHP was likely written in).
This tells me that $a=5; probably means $a is a pointer to an int. PHP's ->
is a dereferencing an extra pointer.
You can think of then as references before they were ever assigned.
references are simply pointers. variable names are simply pointers.
Technically, the reference (pointer) itself should get copied in the process
of cloning if I am correct. I guess I would have to look at that weird code
again. did you find a bug in PHP there?
That article mentions at the end that it's possible to corrupt memory with
references (that's definitely true with C pointers... been there, done
that - ever heard of GPF?).
Navigation:
[Reply to this message]
|