PHP5 introduces the object assignment mechanism. Copy the code as follows :? PhpclassSimpleClass {public $ varadefaultvalue; publicfunctiondisplayVar () {echo $ this-var ;}$ instancenewSimpleClass (); $ assigned $ I
The code is as follows:
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.
The http://www.bkjia.com/PHPjc/324059.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324059.htmlTechArticle code is as follows :? Php class SimpleClass {public $ var = 'A default value'; public function displayVar () {echo $ this-var ;}}$ instance = new SimpleClass (); $ assigned = $ I...