Home > Backend Development > PHP Tutorial > PHP destructor and recycling mechanism

PHP destructor and recycling mechanism

不言
Release: 2023-03-24 20:28:01
Original
1040 people have browsed it

This article introduces the content of PHP destructor and recycling mechanism, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

// ===Notes section 1===

/*
Question
1. Will assigning the object to something else, such as true, destroy the object?
Answer: Yes

2. Code part 4 in 110.php
Why is one destroyed and 2 empty
The last one will appear under the hr line?

Answer: The last one is destroyed because the php page has been executed.
Finally the system recycles and $d is destroyed at this time.
So it is displayed behind the hr line
*/



// ===Code part 1===

class Human2 {

    public $name = null;    public $gender = null;    public function __construct() {
        echo &#39;出生了<br >&#39;;
    }    public function __destruct() {
        echo &#39;再见<br >&#39;;
    }
}$a = new Human2();$b = new Human2();$c = new Human2();$d = new Human2();unset($a);//$b = false;$b = true;//改成true也可以销毁$c = null;echo &#39;<hr >&#39;;
Copy after login



// Object recycling mechanism

// == =Code part 2===

class Human {

    public $name = null;    public $gender = null;    public function __destruct() {
        echo &#39;再见!<br >&#39;;
    }
}$a = new Human();$b = $c = $d = $a;unset($a);echo &#39;<hr >&#39;;
Copy after login

/*
Then the question is:
1. How many times have you died?
2. Will you die on the HR line or off the HR line?

Answer: Die once, below the gray line.
As shown in Figure 11101, one of the keys a, which opens the human house, is missing.
There are also three keys b, c, and d.

Until all codes are finally executed and the recycling mechanism is started,
See you again in the end.
*/
PHP destructor and recycling mechanism
PHP destructor and recycling mechanism



// ===Code part 3===

class Human {

    public $name = &#39;张三&#39;;    public $gender = null;    public function __destruct() {
        echo &#39;再见!<br >&#39;;
    }
}$a = new Human();$b = $c = $d = $a;echo $a->name,&#39;<br >&#39;; 
//张三echo $b->name,&#39;<br >&#39;; //张三$b->name =&#39;李四&#39;;echo $a->name,&#39;<br >&#39;; //李四echo $b->name,&#39;<br >&#39;; //李四unset($a);echo &#39;<hr >&#39;;// hr线// 再见!
Copy after login



// ===Code part 4===

class Human {

    public $name = &#39;张三&#39;;    public $gender = null;    public function __destruct() {
        echo &#39;再见!<br >&#39;;
    }
}$e = $f = $g = new Human();unset($e);echo &#39;unset e<br >&#39;;unset($f);echo &#39;unset f<br >&#39;;unset($g);
// 本行unset触发对象消亡,然后执行下一行echo gecho &#39;unset g<br >&#39;;
Copy after login

/*
Here, the page is finished running, the object is destroyed, and the destructor is executed.
How many objects are destroyed?

Answer:
There is only one object and it only dies once
It dies when the system is recycling, that is, the page is executed, so it is offline.
*/

// ===Notes Part 1===

/*
Question
1. Will assigning the object to something else, such as true, destroy the object?
Answer: Yes

2. Code part 4 in 110.php
Why is one destroyed and 2 empty
The last one will appear under the hr line?

Answer: The last one is destroyed because the php page has been executed.
Finally the system recycles and $d is destroyed at this time.
So it is displayed behind the hr line
*/


Related recommendations:

Detailed explanation of PHP constructor

The above is the detailed content of PHP destructor and recycling mechanism. 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