A destructor is primarily used to release resources, such as freeing a database connection or a picture resource, or destroying an object.
1. Destructors are automatically invoked
2. The main method of destructor is to release resources
3, the destructor call order is: the first object created, and then destroyed.
4. When is the destructor called?
1 when the program exits
2 When an object becomes a garbage object, the destructor of the object is also invoked.
When an object does not have any references to it, it becomes a garbage object, and PHP enables the garbage collector to destroy it.
Format:
function __destruct () {
Actions to free resources
}
Cases:
<?php
Class person{
Public $name;
Public $age;
Constructors
Public function __construct ($name, $age) {
$this->name= $name;
$this->age= $age;
}
destructor, beginning of two underscores
function __destruct () {
echo $this->name. " Destroy the resource, close the database <br/> ";
}
}
$p 1=new person ("www.bianceng.cn", 16);
$p 1=new person ("Dick", 14);
?>