From the PHP Manual:
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. A copy of an already created object can be made by cloning it.
I interpret this description as saying that a variable that contains a instantiated class will affect the original class when modifications are done to the new variable. Meaning, it is not copying the original object. But the examples given are confusing me:
Example 19.5. Object Assignment
<?php
$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);
?>
The above example will output:
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
If $instance is set to equal null, shouldn't $assigned equal null as well? It just said above that it accesses the same instance as the original object that was assigned.
This whole part is confusing to me:
$instance->var = '$assigned will have this value';
$instance = null; // $instance and $reference become null
In the first one, $assigned gets changed, yet in the second one only $instance and $reference change, why? The only thing that comes to mind is that $assigned is a copy, but says it's not. I understand why $reference is being changed because of being passed by reference (&).