Home > Article > Backend Development > The order of execution of __destruct and register_shutdown_function, destruct_PHP tutorial
is based on the analysis of the PHP manual.
__destruct is
<p>析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。</p>
and register_shutdown_function is
<p>Registers a <code>callback</code> to be executed after script execution finishes or exit() is called. 注册一个回调函数,此函数在脚本运行完毕或调用exit()时执行。</p>
Literally understood, __destruct is at the object level, and register_shutdown_function is at the entire script level, so register_shutdown_function should be is a higher level, the functions registered by it should also be executed last. To confirm our guess, we write a script:
<span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'global'<span>;}); </span><span>class</span><span> A { </span><span>public</span> <span>function</span><span> __construct(){ } </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span> } </span><span>new</span> A;
Execution result:
A::__destruct
global
Completely confirmed our guess, it was executed in the order of object->script.
But what if we register register_shutdown_function in the object? Is it still the same order? !
<span>class</span><span> A { </span><span>public</span> <span>function</span><span> __construct(){ </span><span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'local', '<br/>'<span>;});</span> <span> } </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span> } </span><span>new</span> A;
Result:
local
A::__destruct
You can see that register_shutdown_function is called first, and finally the __destruct of the execution object. This indicates that the function registered by register_shutdown_function is treated as a method in the class? ! I don't know, this may require viewing the php source code to parse it.
We can expand the scope to see the situation:
<span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'global', '<br/>'<span>;}); </span><span>class</span><span> A { </span><span>public</span> <span>function</span><span> __construct(){ </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>));</span>
<span></span><span> } </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span>public</span> <span>function</span><span> op() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } } </span><span>class</span><span> B { </span><span>public</span> <span>function</span><span> __construct() { </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>)); </span><span>$obj</span> = <span>new</span><span> A; } </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span>public</span> <span>function</span><span> op() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } } </span><span>$b</span> = <span>new</span> B;
We register a register_shutdown_function function globally, and register another one in class AB, and there are destructor methods in each class. What will be the final result?
global
B::op
A::op
A::__destruct
B::__destruct
The result completely subverted our imagination. No matter whether the register_shutdown_function function is registered in the class or globally, it is executed first. The order of execution in the class is the order in which they are registered. If we study carefully, the global register_shutdown_function function will have this result whether it is placed in front or behind. Things seem to have a result, that is, register_shutdown_function is executed before __destruct, and the global register_shutdown_function function is executed before the register_shutdown_function registered in the class. .
Wait a minute, I cannot accept this result. Based on this conclusion, does it mean that __destruct can be executed after the script has ended? ! Therefore, I will continue to verify this conclusion---remove the register_shutdown_function in the class and retain the global register_shutdown_function:
<span>class</span><span> A { </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span> } </span><span>class</span><span> B { </span><span>public</span> <span>function</span><span> __construct() { </span><span>$obj</span> = <span>new</span><span> A; } </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span> } </span><span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'global', '<br/>';});
Output:
A::__destruct
global
B::__destruct
The results are confusing. There is no doubt about the execution order of the destructors of classes A and B. Because A is called in B, class A must be destroyed before B, but how can the global register_shutdown_function function be sandwiched between them? be executed? ! Puzzling.
According to the analysis of the manual, the destructor can also be executed when exit is called.
<p>析构函数即使在使用 exit()终止脚本运行时也会被调用。在析构函数中调用 exit() 将会中止其余关闭操作的运行。</p>
If exit is called in a function, how are they called?
<span>class</span><span> A { </span><span>public</span> <span>function</span><span> __construct(){ </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>)); </span><span>exit</span><span>; } </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span>public</span> <span>function</span><span> op() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } } </span><span>class</span><span> B { </span><span>public</span> <span>function</span><span> __construct() { </span><span>register_shutdown_function</span>(<span>array</span>(<span>$this</span>, 'op'<span>)); </span><span>$obj</span> = <span>new</span><span> A; } </span><span>public</span> <span>function</span><span> __destruct() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } </span><span>public</span> <span>function</span><span> op() { </span><span>echo</span> <span>__class__</span>,'::',<span>__function__</span>,'<br/>'<span>; } } </span><span>register_shutdown_function</span>(<span>function</span>(){<span>echo</span> 'global', '<br/>'<span>;}); </span><span>$b</span> = <span>new</span> B;
Output:
global
B::op
A::op
B::__destruct
A::__destruct
This sequence is similar to the third example above. What is different and incredible is that the destructor of class B is executed before class A. Is it true that all references to class A are destroyed only after B is destroyed? ! unknown.
Please correct me.
This is a solution taking into account php4 and php5 compatibility issues. .
In the PHP 4 version, the constructor is a method with the same name as the class. In PHP 5, it is the __construct method. This creates a compatibility issue. Under PHP 4, the constructor is the page method. In the PHP 5 version, the constructor is the __construct method.
Two methods in this class exist at the same time so that they can be used normally under both php4 and php5. If this class is run under PHP 4 and the page method is used as the constructor, __construct will be called. So that the real constructor can run normally. .
__destruct method is the destructor in PHP 5. Executed at the end of the code run. .
Check whether this method exists in the page method. If it exists, run this method when the code ends. . The purpose is also to be able to use the destructor normally under PHP 4. .
============================
The explanation of this code has been answered above. . What are the specific functions used for? Please check the php 5 manual. .