PHP SplObjectStorage instance
This article mainly introduces the use of PHP SplObjectStorage 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 spl SplObjectStorage 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:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 |
Class {
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 object to Storage
$ Container-> attach ($ a1 );
$ Container-> attach ($ a2 );
$ Container-> attach ($ a3 );
// SplObjectStorage: detach: removes an object from Storage.
$ Container-> detach ($ a2 );
// SplObjectStorage: contains is used to check whether the object is stored.
Var_dump ($ container-> contains ($ a1); // true
Var_dump ($ container-> contains ($ a4); // false
// Traverse
$ Container-> rewind ();
While ($ container-> valid ()){
Var_dump ($ container-> current ());
$ Container-> next ();
}
|