Copy code 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 rewrites the OOP bottom layer. When a class generates an instance (object), the return value $instance is not the object itself, but just an id (or resource handle) of the object. Therefore, when $instance is assigned to $assigned, $assigned also points to This object is a bit like the reference (&) operation of an ordinary variable. Therefore, when $instance is initialized, $assigned is also initialized. However, when $instance is destroyed (=null), because the corresponding object still has a handle ($assigned), the object will not be destroyed and the destructor will not be triggered. As a result, var_dump($assigned) is the value of the object, and $instance is already an empty handle, showing null. Because $reference has a reference relationship similar to that of ordinary variables with $instance, it also becomes an empty handle and displays null.
The above has introduced the object assignment mechanism of php5 PHP5, including the content of php5. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn