The __destruct method of the class is called when all references to the object are not set.
for example
$dummy = (object) new Class();
The destructor is called automatically if the dummy object is null or the script exits.
unset($dummy); // or $dummy = null; //exit(); //also possible
However, to call the destructor method, there are three important points in memory:
First, the desctructor method must be open, not protected, or closed.
Secondly, refrain from using internal and circular references. For example:
class NewDemo { function __construct() { $this->foo = $this; } function __destruct() {
The following will also not work:
$a = new Class(); $b = new Class(); $a->pointer = $b; $b->pointer = $a; unset($a); // will not call destructor unset($b); // will not call destructor
Thirdly, the decision whether destructors will be called after sending the output. Through
gc_collect_cycles()
You can determine whether all destructors are called before sending data to the user.
See http://php.net/manual/en/language.oop5.decon.php for sources and a detailed explanation of magic destruction methods with examples.
Nitin
source share