This article mainly share with you the PHP destructor and recycling mechanism examples, mainly in the way of code and everyone to share, hope to help everyone.
= = = Note Part 1===
/*
Questions
1, the object is assigned to other, such as true will not destroy the object?
Answer: You can
2, 110.php code Part 4
Why destroy one, empty 2
The last one will appear below the HR line?
A: The last one was destroyed because the PHP page was executed,
Finally, the system is recycled, $d destroyed at this time,
So it appears behind the HR line
*/
= = = Code section 1===
Class Human2 {public $name = null; public $gender = null; Public Function __construct () { echo ' was born <br > '; } Public Function __destruct () { echo ' goodbye <br > '; }} $a = new Human2 (), $b = new Human2 (), $c = new Human2 (), $d = new Human2 (), unset ($a),//$b = false, $b = true;//converted to True can also be destroyed $c = Null;echo '
Recycling mechanism for objects
= = = Code section 2===
Class Human {public $name = null; public $gender = null; Public Function __destruct () { echo ' Goodbye! <br > '; }} $a = new Human (), $b = $c = $d = $a; unset ($a); Echo '
/*
So here's the question:
1. How many times have you died?
2, die on the HR line, or die in the HR line?
A: Dead once, below the gray line.
As shown in 11101, one of the keys to the human house has been opened.
and b,c,d three keys.
Until all the code finishes executing, start the recycle mechanism,
Finally goodbye.
*/
= = = Code section 3===
Class Human {public $name = ' Zhang San '; public $gender = null; Public Function __destruct () { echo ' Goodbye! <br > '; }} $a = new Human (), $b = $c = $d = $a; echo $a->name, ' <br > '; Zhang San Echo $b->name, ' <br > '; Zhang San $b->name = ' John Doe '; Echo $a->name, ' <br > '; John Doe Echo $b->name, ' <br > '; John Doe unset ($a); Echo '
= = = Code section 4===
Class Human {public $name = ' Zhang San '; public $gender = null; Public Function __destruct () { echo ' Goodbye! <br > '; }} $e = $f = $g = new Human (); unset ($e); Echo ' unset e<br > '; unset ($f); Echo ' unset f<br > '; unset ($g);//The Bank unset triggers the Like extinct, then perform the next line of Echo Gecho ' unset g<br > ';
/*
Here, the page runs, the object is destroyed, and the destructor is executed.
How many objects have been destroyed?
For:
Only one object, only 1 times dead.
Die when the system is reclaimed, that is, the page executes, so it is under the HR line.
*/