Introduction of PHP5 's object assignment mechanism _php skills

Source: Internet
Author: User
Copy Code code 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'll 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. When a class generates an instance (object), the return value $instance is not the object itself, but only an ID (or resource handle) of the object, so when $instance is assigned to $assigned, $assigned also points to the object. This is somewhat like a reference (&) operation of a generic variable. So, when the $instance is initialized, the $assigned is initialized. However, when $instance is destroyed (=null), the object is not destroyed and the destructor is not triggered because the corresponding object also has a handle present ($assigned). As a result, Var_dump ($assigned) is the value of the object, and $instance is already an empty handle, showing null. $reference because it has a reference relationship with a common variable like $instance, it also becomes an empty handle and displays null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.