Home >Backend Development >PHP Problem >How to use php destructor method

How to use php destructor method

(*-*)浩
(*-*)浩Original
2019-09-25 15:39:343172browse

How to use php destructor method

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is php parserNext article:what is php parser