-
- class person{
- public function __destruct() {
- echo "
The destructor is executed here";
- echo "
This is generally used Place, close the database, close the file, etc. finishing work ";
- }
- }
- $p = new person();
- for ($i = 0; $i < 5; $i++) {
- echo "< br /> $i";
- }
- //The object is destroyed before the php program ends.
- ?>
-
Copy code
When the object is not pointed to, the object is destroyed.
-
-
- class person {
- public function __destruct() {
- echo "
The destructor is executed here";
- echo "
Generally used here To place, close the database, close the file, etc. ";
- }
- }
- $p = new person();
- $p = null;
- //We see here that the destructor is executed here. .
- for ($i = 0; $i < 5; $i++) {
- echo "
$i";
- }
- ?>
-
Copy code
Set $p Assign $p a string if it is empty or on line 11, so that the object pointed to by $p becomes a garbage object.
PHP will garbage destroy this object.
unset variable
-
-
- class person {
- public function __destruct() {
- echo "
The destructor is executed here ";
- }
- }
- $ p = new person();
- $p1 = $p; //Set the new reference variable $p1 to also point to this object
- unset($p);
- echo "Do you see that /$p is destroyed and the object is also destroyed? What?";
- for ($i = 0; $i < 5; $i++) {
- echo "
$i";
- }
- unset($p1);
- echo "We see Here, the destructor is executed";
- ?>
-
Copy code
When unset a reference variable, unset destroys the variable pointing to the object, not the object.
|