This article mainly introduces the use of PHP splobjectstorage example, Splobjectstorage is the SPL standard library of Data structure object container, used to store a group of objects, especially when you need to uniquely identify the object, you need friends can refer to the following
The PHP SPL splobjectstorage is used to store a set of objects, especially if you need to uniquely identify the object.
The PHP SPL Splobjectstorage class implements countable,iterator,serializable,arrayaccess four interfaces. can achieve statistics, iterations, serialization, array access and other functions.
Look at one of the following simple examples:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
class A {public $i; the public function __construc T ($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 removes the object from the storage->detach ($a 2); //splobjectstorage::contains is used to check whether the object exists in storage var_dump ($container->contains ($a 1)); True Var_dump ($container->contains ($a 4)); False //traversal $container->rewind (); while ($container->valid ()) {Var_dump ($container->current ()); $container->next ();} |