Hi all
As I understand it, object assignment causes the variable to contain
the same object identifier, whereas assigning a variable via reference
creates a true alias of the RHS:
<?php
class A
{
public $foo = 1;
}
$a = new A; //<id1>
$b = $a; // <id1= ($a) = ($b)
$b->foo = 2;
echo "2: ".$a->foo."\n"; // should print out 2
echo "2: ".$b->foo."\n"; // should print out 2
$c = new A; //<id2>
$a = $c; // <id2= ($a) = ($c), <id1= ($b)
echo "1: ".$a->foo."\n"; // should print out 1
echo "2: ".$b->foo."\n"; // should print out 2
echo "1: ".$c->foo."\n"; // should print out 1
So far so good, as expected $a = $c causes $a to hold the same
identifier as $c. If $a and $b were TRUE aliases, then echoing $b
would print 1 (which it doesn't).
Now, onto references
Similar to the PHP manual, I group variables in parentheses that are
true aliases of each other
$A = new A; // <id3>
$B = &$A; // ($A,$B) = <id3>
$B->foo = 2;
echo "2: ".$A->foo."\n"; // should print out 2
echo "2: ".$B->foo."\n"; // should print out 2
as expected
echo "\n";
$C = new A;
$A = $C; // ($C) = ($A,$B) = <id3>
echo "1: ".$A->foo."\n"; // should print out 1
echo "1: ".$B->foo."\n"; // should print out 1
echo "1: ".$C->foo."\n"; // should print out 1
now, different to using assignment, changing $A to hold id3 means that
$B will now hold id3, which means that all 3 should print out 1, which
is what happens... still good.
echo "\n";
$D = new A; // <id4>
$A = &$D; // ($A,$B,$D) = <id4>, ($C) = <id3// shouldn't re-
referncing $A also re-reference $B, as they are aliases of each other?
// actually doing is ($B) = ($C) = <id3>, ($A,$D) = <id4>
$A->foo = 2;
echo "2: ".$A->foo."\n"; // should print out 2
echo "2: ".$B->foo."\n"; // should print out 2
echo "1: ".$C->foo."\n"; // should print out 1
echo "2: ".$D->foo."\n"; // should print out 2
What this actually prints out is 2 1 1 2. Now, when we write $A = &$D,
as $A and $B are aliased, shouldn't they both then point to id4? What
seems to be happening is that $B becomes un-aliased somehow...
Taras
As I understand it, object assignment causes the variable to contain
the same object identifier, whereas assigning a variable via reference
creates a true alias of the RHS:
<?php
class A
{
public $foo = 1;
}
$a = new A; //<id1>
$b = $a; // <id1= ($a) = ($b)
$b->foo = 2;
echo "2: ".$a->foo."\n"; // should print out 2
echo "2: ".$b->foo."\n"; // should print out 2
$c = new A; //<id2>
$a = $c; // <id2= ($a) = ($c), <id1= ($b)
echo "1: ".$a->foo."\n"; // should print out 1
echo "2: ".$b->foo."\n"; // should print out 2
echo "1: ".$c->foo."\n"; // should print out 1
So far so good, as expected $a = $c causes $a to hold the same
identifier as $c. If $a and $b were TRUE aliases, then echoing $b
would print 1 (which it doesn't).
Now, onto references
Similar to the PHP manual, I group variables in parentheses that are
true aliases of each other
$A = new A; // <id3>
$B = &$A; // ($A,$B) = <id3>
$B->foo = 2;
echo "2: ".$A->foo."\n"; // should print out 2
echo "2: ".$B->foo."\n"; // should print out 2
as expected
echo "\n";
$C = new A;
$A = $C; // ($C) = ($A,$B) = <id3>
echo "1: ".$A->foo."\n"; // should print out 1
echo "1: ".$B->foo."\n"; // should print out 1
echo "1: ".$C->foo."\n"; // should print out 1
now, different to using assignment, changing $A to hold id3 means that
$B will now hold id3, which means that all 3 should print out 1, which
is what happens... still good.
echo "\n";
$D = new A; // <id4>
$A = &$D; // ($A,$B,$D) = <id4>, ($C) = <id3// shouldn't re-
referncing $A also re-reference $B, as they are aliases of each other?
// actually doing is ($B) = ($C) = <id3>, ($A,$D) = <id4>
$A->foo = 2;
echo "2: ".$A->foo."\n"; // should print out 2
echo "2: ".$B->foo."\n"; // should print out 2
echo "1: ".$C->foo."\n"; // should print out 1
echo "2: ".$D->foo."\n"; // should print out 2
What this actually prints out is 2 1 1 2. Now, when we write $A = &$D,
as $A and $B are aliased, shouldn't they both then point to id4? What
seems to be happening is that $B becomes un-aliased somehow...
Taras
Comment