测试代码如下:< ? php
class testClass1
{
function __destruct( ) {
printf ( "%s::__destruct()<br/>\n" , get_class ( ) ) ;
}
}class testClass2
{
public $ cass1 ;
function __construct( $ class1 ) {
$ this - > class1 = $ class1 ;
}
function display( ) {
echo "are you ready!<br/>\n" ;
}
function __destruct( ) {
printf ( "%s::__destruct()<br/>\n" , get_class ( ) ) ;
}
}function testfunc( $ class2 )
{
$ class2 - > display( ) ;
exit ( ) ; //第二次测试,唯一要注释的行
}
$ class1 = new testClass1( ) ;
$ class2 = new testClass2( $ class1 ) ;testfunc( $ class2 ) ;
? >
第一次在 testfunc() 中退出:
are you ready!<br/>
testClass1::__destruct()<br/>
testClass2::__destruct()<br/>第二次不在 testfunc() 中退出(注释掉那个exit后):
are you ready!<br/>
testClass2::__destruct()<br/>
testClass1::__destruct()<br/>可以看出这两个不同位置的退出造就了来那个不同的结果,从来理论上讲, 第二次的结果才是我们想要的。
如果你在testClass2::__destruct()是用 testClass1中的一些销毁掉的东西那将是一场噩梦。这应该算一个很可恶的bug
class testClass1
{
function __destruct( ) {
printf ( "%s::__destruct()<br/>\n" , get_class ( ) ) ;
}
}class testClass2
{
public $ cass1 ;
function __construct( $ class1 ) {
$ this - > class1 = $ class1 ;
}
function display( ) {
echo "are you ready!<br/>\n" ;
}
function __destruct( ) {
printf ( "%s::__destruct()<br/>\n" , get_class ( ) ) ;
}
}function testfunc( $ class2 )
{
$ class2 - > display( ) ;
exit ( ) ; //第二次测试,唯一要注释的行
}
$ class1 = new testClass1( ) ;
$ class2 = new testClass2( $ class1 ) ;testfunc( $ class2 ) ;
? >
第一次在 testfunc() 中退出:
are you ready!<br/>
testClass1::__destruct()<br/>
testClass2::__destruct()<br/>第二次不在 testfunc() 中退出(注释掉那个exit后):
are you ready!<br/>
testClass2::__destruct()<br/>
testClass1::__destruct()<br/>可以看出这两个不同位置的退出造就了来那个不同的结果,从来理论上讲, 第二次的结果才是我们想要的。
如果你在testClass2::__destruct()是用 testClass1中的一些销毁掉的东西那将是一场噩梦。这应该算一个很可恶的bug
exit的说明
既然exit了,所有的对象都应该被销毁,顺序已经不重要了,因为exit意味着要收工了
如果业务逻辑依赖这种顺序,你可以显式的控制它,在退出之前,手动销毁对象
我知道这样是可以的,但这个php 应该可以实现这种销毁顺序, 这是一个缺陷
class A
{
public $T;
function __destruct()
{
echo "A\n";
}
};class B
{
public $T;
function __destruct()
{
echo "B\n";
}
};$X = new B;
$Y = new A;$X->T=$Y;
$Y->T=$X;