PHP SPL Splobjectstorage is used to store a set of objects, especially when you need to uniquely identify an object.
The PHP SPL Splobjectstorage class implements the Countable,iterator,serializable,arrayaccess four interfaces. It can realize statistics, iteration, serialization, array-type access and other functions.
Let's look at a simple example:
Class A {public $i; Public function __construct ($i) { $this->i = $i; }} $a 1 = new A (1); $a 2 = new A (2); $a 3 = new A (3); $a 4 = new A (4) ; $container = new Splobjectstorage (); Splobjectstorage::attach add objects to storage $container->attach ($a 1), $container->attach ($a 2); $container Attach ($a 3); Splobjectstorage::d Etach To remove the object from storage $container->detach ($a 2); Splobjectstorage::contains is used to check if an object exists in storage var_dump ($container->contains ($a 1)); Truevar_dump ($container->contains ($a 4)); False//Traverse $container->rewind (), while ($container->valid ()) { var_dump ($container->current ()); $container->next ();}