This article describes the PHP constructor and destructor usage. Share to everyone for your reference, specific as follows:
When a new object is instantiated, both the construction method and the destructor are invoked automatically, and the corresponding method of the parent class is used if there is inheritance.
The destructor is invoked in three different cases:
① uses unset () to destroy an object that is not invoked if there is an object-passing value;
② change the value of the variable to point to the object;
After the ③php program code runs.
<?php
class base{public
$name;
function __construct ($name) {
$this->name = $name;
echo ' obj '. $this->name. ' Have built '. ' </br> '. ' </br> ';
}
function __destruct () {
echo ' obj '. $this->name. ' Have destroyed '. ' </br> '. ' </br> ';
}
}
$a = new base (' a ');
$b = new Base (' B ');
$c = new Base (' C ');
Unset ($b);
$c = ' d ';
The results of the operation are as follows:
Obj A have built
obj b have built
obj c have built
obj B have destroyed
obj C have destroyed
obj A have destroyed
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.