|
Posted by Michael Fesser on 12/19/07 18:09
..oO(Logos)
>On Dec 13, 3:16 pm, Michael Fesser <neti...@gmx.de> wrote:
>
>> At least when working with objects. But nevertheless
>>
>> $foo = new Test();
>> $a = $foo;
>> $b = &$foo;
>>
>> are still different things, even in PHP 5. In some particular situations
>> this might become an issue.
>
>Oooo...errr...ummm...could someone explain how exactly those are
>different when using PHP5, then, please? If everything is done by
>reference for objects, then to me $a and $b both look like pointers to
>an object.
Don't confuse pointers with references, they are entirely different
things. PHP doesn't know pointers.
And correctly spoken objects in PHP 5 are _not_ passed by reference (at
least it's not what PHP calls a reference), even if it's still mentioned
that way on many websites. But it's wrong.
Internally objects are represented by a handle (a simple number), which
is what is moved around when you assign objects to variables, copy them
or pass them to a function. You're never working directly with the
object itself, but with its handle. Of course usually you won't notice
that, because it's handled transparently by PHP.
New Object Model
http://www.php.net/manual/en/migration5.oop.php
| [Objects in PHP 4] The drawback of this method was that semantically
| the whole object was copied when a variable was assigned, or passed as
| a parameter to a method. In the new approach, objects are referenced
| by handle, and not by value (one can think of a handle as an object's
| identifier).
Of course in addition to these object handles there are still the normal
PHP references, which you can use as well. So in the example code above
$a contains a copy of the object handle which was created beforehand,
while $b is a reference to that handle. That's a difference.
Here's a background article regarding this issue (and some more):
You're being lied to.
http://blog.libssh2.org/index.php?/archives/51-Youre-being-lied-to..html
Micha
Navigation:
[Reply to this message]
|