Re: PHP5 and class inheritance question
am 19.12.2007 23:11:39 von Logos
So is this, my current understanding, correct?
$foo = new Test();
$a = $foo;
$b = &$foo;
$foo and $b are both pointing to the same thingie, which in turn
points to Test().
$a is pointing to a different thingie from $foo/$b, which in turn
points to Test().
Changing either $foo OR $b to point to a new thingie will result in
BOTH being changed.
Changing $a to point to a new thingie will leave $foo and $b
unaffected.
Re: PHP5 and class inheritance question
am 20.12.2007 01:12:16 von Michael Fesser
..oO(Logos)
>
Don't worry. In most cases it doesn't matter. But in some rare cases it
might. And then you're looking for an explanation ...
Two years ago this issue came up in a German PHP newsgroup, where a
particular script behaved in a strange and unexpected way. During the
discussion these issues with references and object handles came up. The
conclusion was, that PHP behaved as it should and the object handling
worked as expected. It is (or was) just described in a rather bad and
incomplete way in the manual, hence all the confusion.
>So is this, my current understanding, correct?
>$foo = new Test();
>$a = $foo;
>$b = &$foo;
>
>$foo and $b are both pointing to the same thingie, which in turn
>points to Test().
>$a is pointing to a different thingie from $foo/$b, which in turn
>points to Test().
Yes, the behaviour could be described that way. As said - usually you
don't have to think about these issues.
As an addition: There's an old bug report and also a section in the
manual regarding this issue:
Objects are not being passed by reference
http://bugs.php.net/bug.php?id=32137
http://www.php.net/manual/en/language.oop5.basic.php#languag e.oop5.basic.new
>Changing either $foo OR $b to point to a new thingie will result in
>BOTH being changed.
Yep, that's the nature of a reference.
>Changing $a to point to a new thingie will leave $foo and $b
>unaffected.
Correct.
Micha