如果该测试的结构正确,则似乎表明在销毁存储之前分离物品与在物品仍然连接时销毁存储相比没有明显差异。注释掉 detach() 块不会导致 check() 输出中的任何可见更改。
<?php
class MyStorage extends SplObjectStorage
{
public function __destruct()
{
echo "__destruct() of ";
var_dump($this);
//parent::__destruct(); // there is no SplObjectStorage::__destruct()
}
}
class Foo
{
public function __destruct()
{
echo "__destruct() of ";
var_dump($this);
}
}
function check($message)
{
global $storage, $x, $y, $z;
echo $message, ':', PHP_EOL;
echo '$storage: ', xdebug_debug_zval('storage');
echo '$x : ', xdebug_debug_zval('x');
echo '$y : ', xdebug_debug_zval('y');
echo '$z : ', xdebug_debug_zval('z');
echo PHP_EOL, PHP_EOL;
}
check("Starting environment");
$storage = new MyStorage();
check("\$storage created");
$x = new Foo();
check("\$x created");
$y = new Foo();
check("\$y created");
$z = new Foo();
check("\$z created");
$storage->attach($x);
$storage->attach($y);
$storage->attach($z);
check("Everything is attached");
// comment out this detach() block for comparison testing
$storage->detach($x);
$storage->detach($y);
$storage->detach($z);
check("Everything is detached");
// the check() output here is key for comparing with the final check() output below
$storage = null;
check("$storage destructed");
$x = null;
check("$x destructed");
$y = null;
check("$y destructed");
$z = null;
check("$z destructed");
// final check() output appears here
我认为当我在使用 SplObjectStorage 对象的类中编写显式用户区 detach() 步骤时,造成了我的自我困惑。我认为 PHP Bug #63917 [1] 似乎突出的迭代问题实际上是唯一的错误问题,这首先让我怀疑带有 destruct-with-attachments 场景的错误。
[1] -- http://bugs.php.net/bug.php?id=63917