Home >Backend Development >PHP Problem >What is the name of php destructor method

What is the name of php destructor method

青灯夜游
青灯夜游Original
2023-03-02 19:11:083287browse

The name of the php destructor method is "__destruct()" and cannot have any parameters. The "__destruct()" destructor method will be automatically called only before the object is collected by the garbage collector (that is, before the object is deleted from memory); it allows you to perform some specific operations before destroying an object, such as closing the file, releasing Result set etc.

What is the name of php destructor method

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

The name of the php destructor method is "__destruct ()" and cannot have any parameters.

PHP __destruct(): destructor/method

The role of the destructor is exactly the opposite of the constructor. The function is automatically called only before the object is collected by the garbage collector (that is, before the object is deleted from memory). Destructors allow us to perform certain operations before destroying an object, such as closing files, releasing result sets, etc.

There is a garbage collection mechanism in PHP. When an object cannot be accessed, the garbage collection mechanism will automatically start to reclaim the memory space occupied by the object. The destructor is called before the object is recycled by the garbage collection mechanism.

The declaration format of the destructor is similar to that of the constructor. The name of the destructor declared in the class is also fixed. It also starts with two underscores in the method name __destruct(), and the destructor Cannot take any parameters. The format for declaring a destructor method in a class is as follows:

public function __destruct(){
    ... ...
}

Destructor is not very commonly used in PHP. It is an optional part of the class and is declared in the class only when needed. .

[Example] The following uses an example to demonstrate the use of the destructor.

<?php
    class Website{
        public $name, $url, $title;
        public function __construct(){
            echo &#39;------这里是构造函数------<br>&#39;;
        }
        public function __destruct(){
            echo &#39;------这里是析构函数------<br>&#39;;
        }
    }
    $object = new Website();
    echo &#39;PHP中文网<br>&#39;;
    echo &#39;https://www.php.cn/<br>&#39;;
    echo &#39;脚本运行结束之前会调用对象的析构函数<br>&#39;;
?>

The running results are as follows:

What is the name of php destructor method

#The destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed

Execute before the object is destroyed?

According to the official manual, the destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed. The code demonstration is as follows

class sf{    public function destruct()
    {        echo METHOD . PHP_EOL;
    }
}
 
 
$c1 = new sf;
$c2 = $c1;echo &#39;unset $c2&#39; . PHP_EOL;unset($c2);echo &#39;unset $c1&#39; . PHP_EOL;unset($c1);// ----$c1 = new sf;
$c2 = $c1;echo &#39;null $c2&#39; . PHP_EOL;
$c2 = null;echo &#39;null $c1&#39; . PHP_EOL;
$c1 = null;// ----$c1 = new sf;
$c2 = $c1;echo &#39;123 $c2&#39; . PHP_EOL;
$c2 = 123;echo &#39;456 $c1&#39; . PHP_EOL;
$c1 = 456;echo &#39;the end&#39; . PHP_EOL;

The running result is as follows

What is the name of php destructor method

The destructor will be deleted when all references to an object are deleted or when the object is explicitly destroyed time execution.

Generally speaking, php will destroy the reference when the script ends (not unset) and runs before the script ends.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the name of 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