In the next line, we reserve the memory location in which the new object is stored, and a pointer to it, designated as $instance :
$instance = new SimpleClass();
In the next line, we create a reference to the $instance pointer, marked as $reference :
$reference =& $instance;
In the next line, we reserve a new memory cell, designated as $assigned , different and independent of the one indicated above, in which we store a copy of the pointer (and not the object itself) created above:
$assigned = $instance;
By setting $instance to null , you only disable the pointer to the memory cell that contains the actual object, not the object itself. Any other links to it (for example, $reference ) will also be canceled.
$assigned is an independent copy of a pointer (stored in another memory location) that still points to the actual object.
That is why it can still be used to refer to the actual object.
This comment, found in the PHP documentation, supports what is said above:
<?php class A { public $foo = 1; } $a = new A; $b = $a; $a->foo = 2; $a = NULL; echo $b->foo."\n";
Note
This is a peculiar exception when it comes to memory management. If copying a variable or array is only addressed using the = operator, you must explicitly use the clone keyword with objects, as indicated in the PHP documentation:
An exception to the usual assignment by behavior value in PHP occurs with objects that are referenced in PHP 5. Objects can be explicitly copied using the clone keyword.
Luca fagioli
source share