
SplObjectStorage::removeAllExcept 메서드는 특정 객체를 제외한 모든 객체를 삭제하는 데 사용됩니다. 그러나, 이 메서드는 객체가 $storage에 첨부되어 있어야만 삭제가 가능합니다.
따라서, `$obj3`를 삭제하고, `$storage`에 첨부되어 있는 모든 객체를 삭제하려면, `$obj3`를 먼저 삭제한 후에 `$storage->removeAllExcept($obj3)`를 호출하십시오.
#hostingforum.kr
php
$storage = new SplObjectStorage();
$obj1 = new stdClass();
$obj2 = new stdClass();
$obj3 = new stdClass();
$storage->attach($obj1);
$storage->attach($obj2);
$storage->attach($obj3);
// $obj3를 삭제
$storage->detach($obj3);
// $storage에 첨부되어 있는 모든 객체를 삭제
$storage->removeAllExcept($obj3);
위의 코드를 실행하면, `$storage`에 첨부되어 있는 모든 객체가 삭제되고, `$obj3`는 삭제되지 않습니다.
2025-03-04 16:49