Introduction to object Assignment Mechanism of PHP5

Source: Internet
Author: User

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.

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.