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 — Remover objetos excepto los contenidos en otro almacenamiento del almacenamiento actual
Remover todos los objetos excepto los contenidos en otro almacenamiento del almacenamiento actual.
storageEl almacenamiento que contiene los elementos a mantener en el almacenamiento actual.
Devuelve el número de objetos restantes.
Ejemplo #1 Ejemplo 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));
?>Resultado del ejemplo anterior es similar 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());