
SplObjectStorage::removeAll 메서드는 SplObjectStorage 내의 모든 항목을 삭제합니다.
이 메서드를 호출하면, SplObjectStorage 객체 내의 모든 항목이 삭제되며, 객체 자체도 삭제되지 않습니다.
예를 들어, SplObjectStorage 객체를 생성하고 removeAll() 메서드를 호출했을 때, 객체 내의 모든 항목이 삭제되는지 확인할 수 있는 방법은 다음과 같습니다.
#hostingforum.kr
php
$storage = new SplObjectStorage();
$obj1 = new stdClass();
$obj2 = new stdClass();
$storage->attach($obj1);
$storage->attach($obj2);
var_dump($storage->count()); // 2
$storage->removeAll();
var_dump($storage->count()); // 0
또한, removeAll() 메서드를 호출하기 전에 저장된 항목을 삭제하는 다른 방법은 다음과 같습니다.
#hostingforum.kr
php
$storage = new SplObjectStorage();
$obj1 = new stdClass();
$obj2 = new stdClass();
$storage->attach($obj1);
$storage->attach($obj2);
while ($storage->count()) {
$storage->detach($storage->rewind());
}
위의 예제에서, detach() 메서드를 사용하여 하나씩 항목을 삭제합니다.
이러한 방법으로, removeAll() 메서드를 호출하기 전에 저장된 항목을 삭제할 수 있습니다.
제가 잘못 이해한 부분이 있다면 알려주세요.
2025-05-05 14:19