This article mainly introduces the use of PHPSplObjectStorage instances. SplObjectStorage is the data structure object container in the SPL standard library, used to store a group of objects, especially when you need to uniquely identify objects, for more information, see PHP SplObjectStorage. It is used to store a group of objects, especially when you need to uniquely identify objects.
The PHP SplObjectStorage class implements the Countable, Iterator, Serializable, and ArrayAccess interfaces. Supports statistics, iteration, serialization, array access, and other functions.
Let's look at the following simple example:
Class A {public $ I; public function _ construct ($ I) {$ this-> I = $ I ;}$ a1 = new A (1 ); $ a2 = new A (2); $ a3 = new A (3); $ a4 = new A (4); $ container = new SplObjectStorage (); // SplObjectStorage :: attach add objects to Storage $ container-> attach ($ a1); $ container-> attach ($ a2); $ container-> attach ($ a3); // SplObjectStorage:: detach remove $ container-> detach ($ a2) from Storage; // SplObjectStorage :: contains is used to check whether the object has var_dump ($ container-> contains ($ a1) in Storage; // truevar_dump ($ container-> contains ($ a4 )); // false // traverse $ container-> rewind (); while ($ container-> valid () {var_dump ($ container-> current ()); $ container-> next ();}