$a = new A;
$b = $a;
$b->foo = 'bar'; // changes the object that the identifier in $a and $b both refer to
$b = new B; // overwrites $b with a new object identifier
$b->foo = 'baz'; // changes only $b's object
$a->foo // bar
$b->foo // baz
Contrast that with:
$a = new A;
$b = &$a; // BIG DIFFERENCE HERE!
$b->foo = 'bar'; // changes the object that both $a and $b refer to
$b = new B; // overwrites both $a and $b with a new object identifier
$b->foo = 'baz';
$a->foo // baz
$b->foo // baz
Variable references (&
) work as they always work, you make two variables refer to the same value. Changing either variable changes both variables, they're more or less linked together.
The thing about objects being just identifiers just means that if you copy an object to another variable ($a = $b
), both variables hold the same object identifier. But both variables are not linked in the same way they are when using &
references.
An object identifier is pretty literally something that identifies an object. It's a value like the integer 42
. It's a value that says "I'm referring to object #1". The actual object itself is stored elsewhere and fetched when needed. Object identifiers are one level of indirection:
$a = 42;
$b = &$a;
Here both variables refer to the exact same value. In the symbol table this looks like:
symbol | value
-------+------
a, b | 42
But with object identifiers it looks like this:
$a = new A;
$b = $a;
symbol | value object | details
-------+--------- ---------+--------
a | object#1 object#1 | class A { ... }
b | object#1
(The tables above are mostly for illustrational purposes, actual technical details may differ.)