<?php
class SimpleClass
{
public $var1;
}
$instance = new SimpleClass();
$assigned = $instance;
$reference =& $instance;
$instance->var1 = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
exit;
?>
Can any one help? How come the output of above code is:
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
I can understand NULL for $instance and $reference but how come $assigned not became NULL.
As per my understanding in PHP 5 object are pass by reference, so $assigned is also contain reference, in this case it should also become NULL.
In addition to my understanding, written in PHP manual is “When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. ”
Can any one explain?
Below line are from PHP manual Object and reference
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn’t contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.