About the relationship between object references and destructors in php

黄舟
Release: 2023-03-12 10:52:02
Original
1246 people have browsed it

In php, constructor and destructor both belong to magic methods. For example, if the constructor is in a certain class, when this class It will be automatically called when it is instantiated, and the destructor is automatically called when the object of this class is destroyed. By default, it is automatically called when the program execution ends.

If we make a reference to the object, then the destructor call will also change. If only one object is instantiated, such as $obj, then as long as there is $obj = null; this statement means that the object $obj is destroyed. At this time, the destructor will be called in advance, but other objects of this class will not be affected.

If we use $obj1 = &$obj; that is, use the & symbol to refer to the assignment operation, this operation will not generate a new reference to the object. The so-called two objects are actually Using the same memory heap space, when one of $obj or $obj1 is destroyed, the other will also disappear at the same time.

If you use $obj1 = $obj; operation, this is a direct assignment operation, which will generate a new reference to an object. When either $obj or $obj1 is destroyed, , the destructor will not be executed, and the other object can still be used normally. The destructor will not be executed until both are destroyed. This shows that the two have a unified relationship, not two objects, but a reference relationship. This reference operation does not create a new object that does not interfere with each other, but refers to an object. When the members of one object change, the members of the other object change accordingly and are related to each other, so direct assignment with = is between = A situation between & operation and clone operation.

If you perform a clone operation, it is $obj1 = clone $obj; in this operation, $obj1 and $obj are two objects without any interference. Changing a member will not cause any interference. Affects another member. In clone, all member attributes values ​​of $obj1 are exactly the same as $obj. This is true replication. When copying, you can also write the clone() magic method in the class to give a new value to some attributes of $obj1 or to block unwanted values. The implementation method is simple as an example.

class Player{
    public $name;
    function destruct(){
        echo "Destroying ".$this->name."<br />";
    }
    //设置魔术方法,赋值时会自动调用这里的属性值,作用是初始化值,或屏蔽不想要的值
    function clone(){
        $this->name="gbcs";
    }
}
$james=new Player();
$james2=clone $james;
echo $james2->name.&#39;<br />&#39;;
$james->name="james";
$james2->name="james2";
echo $james->name.&#39;<br />&#39;;
$james=null;
echo $james2->name.&#39;<br />&#39;;
Copy after login

The execution results are as follows:

## It can be clearly seen from the execution results that clone generates two independent objects, and the magic method clone() is also called. It can also be seen from the destructor execution sequence that the two objects are destroyed and assigned separately. The meaning is different.

The above is the detailed content of About the relationship between object references and destructors in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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