©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PHP 5 >= 5.1.0)
SplObjectStorage::detach — Removes an object from the storage
$object
)Removes the object from the storage.
object
The object to remove.
没有返回值。
Example #1 SplObjectStorage::detach() example
<?php
$o = new StdClass ;
$s = new SplObjectStorage ();
$s -> attach ( $o );
var_dump ( count ( $s ));
$s -> detach ( $o );
var_dump ( count ( $s ));
?>
以上例程的输出类似于:
int(1) int(0)
[#1] alan dot bem at gmail dot com [2013-09-07 07:55:45]
SplObjectSotage::detach() has a bug - it rewinds internal array pointer.
Remember that - when looping over the storage - as it has no workaround.
https://bugs.php.net/bug.php?id=65629&edit=2
[#2] r dot wilczek at web-appz dot de [2010-04-30 01:18:38]
Detaching the current entry from the storage prevents SplObjectStorage::next() to operate.
Example as a PHPUnit-test:
<?php
public function testDetachingCurrentPreventsNext()
{
$storage = new SplObjectStorage;
$storage->attach(new stdClass);
$storage->attach(new stdClass);
$storage->rewind();
$iterated = 0;
$expected = $storage->count();
while ($storage->valid()) {
$iterated++;
$storage->detach($storage->current());
$storage->next();
}
$this->assertEquals($expected, $iterated);
}
?>
This test will fail, for the iteration will never reach the second stdClass.
SplObjectStorage::next() obviously relies on the current element to be valid.
If you want to detach objects during iterations, you should dereference objects, before you call next() and detach the reference after next():
<?php
public function testDetachingReferenceAfterNext()
{
$storage = new SplObjectStorage;
$storage->attach(new stdClass);
$storage->attach(new stdClass);
$storage->rewind();
$iterated = 0;
$expected = $storage->count();
while ($storage->valid()) {
$iterated++;
$object = $storage->current();
$storage->next();
$storage->detach($object);
}
$this->assertEquals($expected, $iterated);
}
?>
This test will pass.