Destructor (method) (Recommended learning: PHP programming from entry to proficiency)
__destruct ( void ) : void
PHP 5 The concept of destructors is introduced, similar to other object-oriented languages such as C. A destructor is executed when all references to an object are removed or when the object is explicitly destroyed.
Destructor example
<?php class MyDestructableClass { function __construct() { print "In constructor\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "Destroying " . $this->name . "\n"; } } $obj = new MyDestructableClass(); ?>
The destructor is called even when exit() is used to terminate the script running. Calling exit() in the destructor will abort the rest of the shutdown operation.
Note:
The destructor is called when the script is closed, after all HTTP headers have been sent. It is possible that the working directory when the script is closed is different from when it is in a SAPI (such as apache).
Note:
Attempting to throw an exception in the destructor (which is called when the script terminates) will result in a fatal error.
The above is the detailed content of How to use php destructor method. For more information, please follow other related articles on the PHP Chinese website!