Heim > Artikel > Backend-Entwicklung > __destruct与register_shutdown_function执行的先后顺序问题,destruct_PHP教程
根据php手册的解析。
__destruct是
<p>析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。</p>
而register_shutdown_function是
<p>Registers a <code>callback</code> to be executed after script execution finishes or exit() is called. 注册一个回调函数,此函数在脚本运行完毕或调用exit()时执行。</p>
从字面上理解,__destruct是对象层面的,而register_shutdown_function是整个脚本层面的,理应register_shutdown_function的级别更高,其所注册的函数也应最后执行。为证实我们的猜测,我们写一段脚本:
<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;
执行结果:
A::__destruct
global
完全证实了我们的猜测,它按照对象->脚本的顺序被执行了。
但如果我们在对象中注册了register_shutdown_function呢?它还是一样的顺序吗?!
<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;
结果:
local
A::__destruct
可以看到register_shutdown_function先被调用了,最后才是执行对象的__destruct。这表明register_shutdown_function注册的函数被当作类中的一个方法?!不得而知,这可能需要查看php源代码才能解析了。
我们可以扩大范围查看情况:
<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;
我们在全局注册一个register_shutdown_function函数,在类AB中又各注册了一个,而且类中分别还有析构方法。最后运行结果会怎样呢?
global
B::op
A::op
A::__destruct
B::__destruct
结果完全颠覆了我们的想像,register_shutdown_function函数无论在类中注册还是在全局注册,它都是先被执行,类中执行的顺序就是它们被注册的先后顺序。如果我们再仔细研究,全局的register_shutdown_function函数无论放在前面还是后面都是这个结果,事情似乎有了结果,那就是register_shutdown_function比__destruct先执行,全局的register_shutdown_function函数又先于类中注册的register_shutdown_function先执行。
且慢,我无法接受这个结果,按照这样的结论,难道说脚本已经结束后还可以再执行__destruct?!因此,我还要继续验证这个结论---去掉类中注册register_shutdown_function,而保留全局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/>';});
输出:
A::__destruct
global
B::__destruct
结果令人茫然,A、B两个类的析构函数执行顺序无可质疑,因为B中调用了A,类A肯定比B先销毁,但全局的register_shutdown_function函数又怎么夹在它们中间被执行?!费解。
按照手册的解析,析构函数也可在调用exit时执行。
<p>析构函数即使在使用 exit()终止脚本运行时也会被调用。在析构函数中调用 exit() 将会中止其余关闭操作的运行。</p>
如果在函数中调用exit,它们又如何被调用的呢?
<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;
输出:
global
B::op
A::op
B::__destruct
A::__destruct
这个顺序与上述第三个例子相似,不同的且令人不可思议的是B类的析构函数先于类A执行,难道销毁B后类A的所有引用才被全部销毁?!不得而知。
敬请大家指正。
这是考虑到了 php4 和 php5 兼容性问题的解决方案。。
在 php 4 版本中,构造函数是和类名相同的方法。而 php 5 中则是 __construct 方法。这就造成了一个兼容性问题。在 PHP 4 版本下,构造函数是 page 方法。而 php 5版本下,构造函数是 __construct 方法。
这个类里两个方法同时存在,为的就是能在 php4 和 php5下都可正常使用。如果是 php 4 下运行这个类,page 方法做为构造函数,就会把 __construct 调过来用。使得真正的构造函数能正常运行。。
__destruct 方法是 php 5 里的析构函数。在代码运行结束时执行。。
page 方法里检查这个方法是否存在,如果存在就代码结束时运行这个方法。。目的也是为了能在 PHP 4 下正常使用 析构函数。。
===========================
这段代码的解释在上面已经回答了。。具体那几个函数做什么用。请查 php 5 手册。。