Copy codeThe Code is as follows:
<? Php
Class SimpleClass {
Public $ var = 'a default value ';
Public function displayVar (){
Echo $ this-> var;
}
}
$ 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 );
Var_dump ($ instance );
Var_dump ($ assigned );
Var_dump ($ reference );
?>
Php5 overrides the underlying OOP. When the class generates an instance (object), the returned value $ instance is not the object itself, but an id (or resource handle) of the object. Therefore, when $ instance is assigned to $ assigned, $ assigned also points to this object, which is a bit like the reference (&) operation of common variables. Therefore, $ assigned is also initialized when $ instance is initialized. However, when $ instance is destroyed (= null), because the corresponding object still has a handle ($ assigned), the object will not be destroyed, the Destructor will not be triggered either. As a result, var_dump ($ assigned) is the object value, while $ instance is an empty handle, and null is displayed. Because $ reference has a reference relationship similar to $ instance, it also becomes a null handle and displays null.