The following is pretty odd:

<?php
$a = new stdClass;
$b = $a;

$a->foo = 'bar';
var_dump($b);
/* Notice at this point, that $a and $b are,
* indeed sharing the same object instance.
* This is their reference-like behavior at work.
*/

$a = 'baz';
var_dump($b);
/* Notice now, that $b is still that original object.
* Had it been an actual reference with $a,
* it would have changed to a simple string as well.
*/
?>

This is how it's supposed to work. $b is not a reference to $a, $b is
a reference to the object referenced by $a (*objects are referenced,
not variables*). Thus when $a is changed to refer to a string, $b
still refers to the original object referenced by $a.

The odd bit is that he wrote the article as if he expected it to
behave differently (ie, that $b would become a reference to $a which
would be a reference to the object), which is just weird. You were
never lied to, PHP5 references objects, not other references.

Mostly though, it's a very poor idea to rely on reference or copy
semantics if you can avoid it - it's a world of subtle bugs waiting to
happen.  Very occasionally it is necessary for performance (mostly
sorting, trees or large object handling), the rest of the time you
shouldn't need to worry about it.

On Mon, Jan 11, 2010 at 2:48 PM, ctx2002 <[email protected]> wrote:
> http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html#serendipity_CommentForm
>
> http://blog.libssh2.org/index.php?/archives/51-Youre-being-lied-to..html
>
> interestingtopic about reference
>
> --
> NZ PHP Users Group: http://groups.google.com/group/nzphpug
> To post, send email to [email protected]
> To unsubscribe, send email to
> [email protected]
>
-- 
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]

Reply via email to