About the relationship between object references and destructors in PHP

Source: Internet
Author: User
constructors and destructors in PHP are magic methods, such as constructors in a class that are automatically invoked when the class is instantiated, and the destructor is automatically called when the object of the class is destroyed, by default at the end of the program execution.

If we refer to the object, then the destructor call will also change, if only an object is instantiated, such as a $obj so long as there is $obj = null, this statement means that the object $obj is destroyed, this time the destructor is called early, but the other objects of this class are not affected.

If we use $obj1 = & $obj, even if we refer to an assignment with the & symbol, this operation does not produce a new reference to the object, so the so-called two objects actually use the same memory heap space, and when $obj or $obj1 one is destroyed, the other disappears at the same time.

If you use $obj1 = $obj; operation, this is a direct assignment operation, will produce a new reference to an object, when $obj or $obj1 one of them is destroyed, the destructor will not be executed, another object can be normal use, two are destroyed, the destructor will be executed, This shows that the two are still a unified relationship, not two objects, but a reference relationship, the reference operation is not to create a new object that does not interfere with each other, but to refer to an object, when the members of one of the objects change, the other object's members are changed and associated with each other, so directly with = assignment is between = A situation between the & operation and the clone operation.

If the clone operation is $obj1 = Clone $obj, this operation, $obj 1 and $obj is two objects, without any interference, change one member does not affect another member, in clone is $obj1 all member property values are exactly and $obj, This is the true meaning of replication. Replication can also be in the class to write the Clone () Magic method, to $obj1 some properties of a new value or mask the unwanted value, the implementation of the method is simple to cite an example.

Class player{public    $name;    function Destruct () {        echo "destroying". $this->name. " <br/> ";    }    Set the Magic method, the assignment will automatically call the property value here, the function is to initialize the value, or mask the unwanted value    function clone () {        $this->name= "Gbcs";    }} $james =new Player (), $james 2=clone $james, Echo $james 2->name. ' <br/> '; $james->name= "James"; $james 2->name= "James2"; Echo $james->name. ' <br/> '; $james =null;echo $james 2->name. ' <br/> ';

The results of the implementation are as follows:

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.