<?php
$instance = new SimpleClass();
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
what is the difference between below given 2 lines?
$assigned = $instance;
$reference =& $instance;
as in OOP object is by default assign by reference . so $assigned will also have
& $instance.
output of above code is
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
This is not totally true.
What the
$instanceholds is the value of the object id, when you assign an object to another variable, you only passed the object id.So when you do
$assigned = $instance;, you are passing the object id which$instanceholds to the variable$assigned, and$instance = nullis only set the variable$instanceto null, nothing will affect to$assigned.But when you do
$reference =& $instance, you are creating a reference to the variable$instance, so if you set$instanceto null,$referencewill also be null.