This article mainly shares with you detailed examples of PHP destructor and recycling mechanism. It is mainly shared with you in the form of code. I hope it can help you.
// ===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 two 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 '出生了<br >'; } public function __destruct() { echo '再见<br >'; } }$a = new Human2();$b = new Human2();$c = new Human2();$d = new Human2();unset($a);//$b = false;$b = true;//改成true也可以销毁$c = null;echo '<hr >';
// Object recycling mechanism
// ===Code part 2===
class Human { public $name = null; public $gender = null; public function __destruct() { echo '再见!<br >'; } }$a = new Human();$b = $c = $d = $a;unset($a);echo '<hr >';
/*
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.
*/
##// ===Code part 3===
class Human { public $name = '张三'; public $gender = null; public function __destruct() { echo '再见!<br >'; } }$a = new Human();$b = $c = $d = $a;echo $a->name,'<br >'; //张三echo $b->name,'<br >'; //张三$b->name ='李四';echo $a->name,'<br >'; //李四echo $b->name,'<br >'; //李四unset($a);echo '<hr >';// hr线// 再见!
// ===Code part 4===
class Human { public $name = '张三'; public $gender = null; public function __destruct() { echo '再见!<br >'; } }$e = $f = $g = new Human();unset($e);echo 'unset e<br >';unset($f);echo 'unset f<br >';unset($g);// 本行unset触发对象消亡,然后执行下一行echo gecho 'unset g<br >';
Here, the page is finished running, the object is destroyed, and the destructor is executed.
How many objects are destroyed?
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.
*/
php Detailed explanation of constructor and destructor
php Detailed explanation of the usage of constructor and destructor
Master the php garbage collection mechanism
The above is the detailed content of Detailed explanation of PHP destructor and recycling mechanism examples. For more information, please follow other related articles on the PHP Chinese website!