Home  >  Article  >  Backend Development  >  What does php destructor mean?

What does php destructor mean?

藏色散人
藏色散人Original
2021-10-28 10:37:402613browse

php destructor refers to the destructor "__destruct()"; the destructor is a concept introduced in PHP5; the destructor will be used when all references to an object are deleted or when the object is displayed Executed when the formula is destroyed.

What does php destructor mean?

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

Destructor

__destruct(): void

PHP 5 introduced the concept of destructor, which is 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";
    }
    function __destruct() {
        print "Destroying " . __CLASS__ . "\n";
    }
}
$obj = new MyDestructableClass();

Like the constructor, the destructor of the parent class will not be called secretly by the engine. To execute the parent class's destructor, parent::__destruct() must be explicitly called in the child class's destructor body. In addition, just like the constructor, the subclass will inherit the parent class if it does not define a destructor.

The destructor is called even when the script is terminated using exit(). Calling exit() in the destructor will abort the remaining shutdown operations.

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. [Recommended learning: "PHP Video Tutorial"]

For detailed information on destructors and constructors, please see the official website: https://www.php.net/manual /zh/language.oop5.decon.php

The above is the detailed content of What does php destructor mean?. 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