The example Kirk provided is not the best option, there's a method "removeAll" that you can pass the SplObjectStore itself, and as the name says, remove all stored objects.(PHP 5 >= 5.3.6, PHP 7, PHP 8)
SplObjectStorage::removeAllExcept — Remove todos os objetos, exceto aqueles contidos em outro armazenamento, do armazenamento atual
Remove todos os objetos, exceto aqueles contidos em outro armazenamento, do armazenamento atual.
storageO armazenamento contendo os elementos a serem retidos no armazenamento atual.
Retorna o número de objetos restantes.
Exemplo #1 Exemplo de SplObjectStorage::removeAllExcept()
<?php
$a = (object) 'a';
$b = (object) 'b';
$c = (object) 'c';
$foo = new SplObjectStorage;
$foo->attach($a);
$foo->attach($b);
$bar = new SplObjectStorage;
$bar->attach($b);
$bar->attach($c);
$foo->removeAllExcept($bar);
var_dump($foo->contains($a));
var_dump($foo->contains($b));
?>O exemplo acima produzirá algo semelhante a:
bool(false) bool(true)
The example Kirk provided is not the best option, there's a method "removeAll" that you can pass the SplObjectStore itself, and as the name says, remove all stored objects.You may remove all elements by passing empty SplObjectStorage
$splStorage->removeAllExcept(new SplObjectStorage());