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

How to use php destructor method

(*-*)浩
Release: 2023-02-24 21:30:02
Original
3150 people have browsed it

How to use php destructor method

Destructor (method) (Recommended learning: PHP programming from entry to proficiency)

__destruct ( void ) : void
Copy after login

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();
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template