Home > Backend Development > PHP Tutorial > Introduction to the object assignment mechanism of PHP5_PHP Tutorial

Introduction to the object assignment mechanism of PHP5_PHP Tutorial

WBOY
Release: 2016-07-21 15:26:03
Original
862 people have browsed it

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 bottom layer of OOP. 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324059.htmlTechArticleCopy the code as follows: ?php class SimpleClass{ public $var = 'a default value'; public function displayVar( ) { echo $this-var; } } $instance = new SimpleClass(); $assigned = $i...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template