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.
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 '------这里是构造函数------<br>'; } public function __destruct(){ echo '------这里是析构函数------<br>'; } } $object = new Website(); echo 'PHP中文网<br>'; echo 'https://www.php.cn/<br>'; echo '脚本运行结束之前会调用对象的析构函数<br>'; ?>
The running results are as follows:
#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 'unset $c2' . PHP_EOL;unset($c2);echo 'unset $c1' . PHP_EOL;unset($c1);// ----$c1 = new sf; $c2 = $c1;echo 'null $c2' . PHP_EOL; $c2 = null;echo 'null $c1' . PHP_EOL; $c1 = null;// ----$c1 = new sf; $c2 = $c1;echo '123 $c2' . PHP_EOL; $c2 = 123;echo '456 $c1' . PHP_EOL; $c1 = 456;echo 'the end' . PHP_EOL;
The running result is as follows
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!