xref: /PHP-8.4/ext/spl/tests/observer_006.phpt (revision 11648c00)
1--TEST--
2SPL: SplObjectStorage with accociatied information
3--FILE--
4<?php
5
6class TestClass
7{
8    public $test = 25;
9
10    public function __construct($test = 42)
11    {
12        $this->test = $test;
13    }
14}
15
16class MyStorage extends SplObjectStorage
17{
18    public $bla = 25;
19
20    public function __construct($bla = 26)
21    {
22        $this->bla = $bla;
23    }
24}
25
26$storage = new MyStorage();
27
28foreach(array(1=>"foo",2=>42) as $key => $value)
29{
30     $storage->attach(new TestClass($key), $value);
31}
32
33var_dump(count($storage));
34
35foreach($storage as $object)
36{
37    var_dump($object->test);
38}
39
40var_dump($storage);
41
42var_dump(serialize($storage));
43echo "===UNSERIALIZE===\n";
44
45$storage2 = unserialize(serialize($storage));
46
47var_dump(count($storage2));
48
49foreach($storage2 as $object)
50{
51    var_dump($object->test);
52}
53
54var_dump($storage2);
55$storage->attach(new TestClass(3), new stdClass);
56$storage->attach(new TestClass(4), new TestClass(5));
57echo "===UNSERIALIZE2===\n";
58var_dump(unserialize(serialize($storage)));
59$storage->rewind();
60$storage->next();
61var_dump($storage->key());
62var_dump($storage->current());
63var_dump($storage->getInfo());
64$storage->setInfo("bar");
65var_dump($storage->getInfo());
66echo "===UNSERIALIZE3===\n";
67var_dump(unserialize(serialize($storage)));
68$storage->rewind();
69$storage->next();
70$storage->next();
71var_dump($storage->key());
72var_dump($storage->current());
73$storage->attach($storage->current(), "replaced");
74echo "===UNSERIALIZE4===\n";
75var_dump(unserialize(serialize($storage)));
76
77?>
78--EXPECTF--
79int(2)
80int(1)
81int(2)
82object(MyStorage)#%d (2) {
83  ["bla"]=>
84  int(26)
85  ["storage":"SplObjectStorage":private]=>
86  array(2) {
87    [0]=>
88    array(2) {
89      ["obj"]=>
90      object(TestClass)#%d (1) {
91        ["test"]=>
92        int(1)
93      }
94      ["inf"]=>
95      string(3) "foo"
96    }
97    [1]=>
98    array(2) {
99      ["obj"]=>
100      object(TestClass)#%d (1) {
101        ["test"]=>
102        int(2)
103      }
104      ["inf"]=>
105      int(42)
106    }
107  }
108}
109string(%d) "%s"
110===UNSERIALIZE===
111int(2)
112int(1)
113int(2)
114object(MyStorage)#%d (2) {
115  ["bla"]=>
116  int(26)
117  ["storage":"SplObjectStorage":private]=>
118  array(2) {
119    [0]=>
120    array(2) {
121      ["obj"]=>
122      object(TestClass)#%d (1) {
123        ["test"]=>
124        int(1)
125      }
126      ["inf"]=>
127      string(3) "foo"
128    }
129    [1]=>
130    array(2) {
131      ["obj"]=>
132      object(TestClass)#%d (1) {
133        ["test"]=>
134        int(2)
135      }
136      ["inf"]=>
137      int(42)
138    }
139  }
140}
141===UNSERIALIZE2===
142object(MyStorage)#%d (2) {
143  ["bla"]=>
144  int(26)
145  ["storage":"SplObjectStorage":private]=>
146  array(4) {
147    [0]=>
148    array(2) {
149      ["obj"]=>
150      object(TestClass)#%d (1) {
151        ["test"]=>
152        int(1)
153      }
154      ["inf"]=>
155      string(3) "foo"
156    }
157    [1]=>
158    array(2) {
159      ["obj"]=>
160      object(TestClass)#%d (1) {
161        ["test"]=>
162        int(2)
163      }
164      ["inf"]=>
165      int(42)
166    }
167    [2]=>
168    array(2) {
169      ["obj"]=>
170      object(TestClass)#%d (1) {
171        ["test"]=>
172        int(3)
173      }
174      ["inf"]=>
175      object(stdClass)#%d (0) {
176      }
177    }
178    [3]=>
179    array(2) {
180      ["obj"]=>
181      object(TestClass)#%d (1) {
182        ["test"]=>
183        int(4)
184      }
185      ["inf"]=>
186      object(TestClass)#%d (1) {
187        ["test"]=>
188        int(5)
189      }
190    }
191  }
192}
193int(1)
194object(TestClass)#%d (1) {
195  ["test"]=>
196  int(2)
197}
198int(42)
199string(3) "bar"
200===UNSERIALIZE3===
201object(MyStorage)#%d (2) {
202  ["bla"]=>
203  int(26)
204  ["storage":"SplObjectStorage":private]=>
205  array(4) {
206    [0]=>
207    array(2) {
208      ["obj"]=>
209      object(TestClass)#%d (1) {
210        ["test"]=>
211        int(1)
212      }
213      ["inf"]=>
214      string(3) "foo"
215    }
216    [1]=>
217    array(2) {
218      ["obj"]=>
219      object(TestClass)#%d (1) {
220        ["test"]=>
221        int(2)
222      }
223      ["inf"]=>
224      string(3) "bar"
225    }
226    [2]=>
227    array(2) {
228      ["obj"]=>
229      object(TestClass)#%d (1) {
230        ["test"]=>
231        int(3)
232      }
233      ["inf"]=>
234      object(stdClass)#%d (0) {
235      }
236    }
237    [3]=>
238    array(2) {
239      ["obj"]=>
240      object(TestClass)#%d (1) {
241        ["test"]=>
242        int(4)
243      }
244      ["inf"]=>
245      object(TestClass)#%d (1) {
246        ["test"]=>
247        int(5)
248      }
249    }
250  }
251}
252int(2)
253object(TestClass)#7 (1) {
254  ["test"]=>
255  int(3)
256}
257===UNSERIALIZE4===
258object(MyStorage)#%d (2) {
259  ["bla"]=>
260  int(26)
261  ["storage":"SplObjectStorage":private]=>
262  array(4) {
263    [0]=>
264    array(2) {
265      ["obj"]=>
266      object(TestClass)#%d (1) {
267        ["test"]=>
268        int(1)
269      }
270      ["inf"]=>
271      string(3) "foo"
272    }
273    [1]=>
274    array(2) {
275      ["obj"]=>
276      object(TestClass)#%d (1) {
277        ["test"]=>
278        int(2)
279      }
280      ["inf"]=>
281      string(3) "bar"
282    }
283    [2]=>
284    array(2) {
285      ["obj"]=>
286      object(TestClass)#%d (1) {
287        ["test"]=>
288        int(3)
289      }
290      ["inf"]=>
291      string(8) "replaced"
292    }
293    [3]=>
294    array(2) {
295      ["obj"]=>
296      object(TestClass)#%d (1) {
297        ["test"]=>
298        int(4)
299      }
300      ["inf"]=>
301      object(TestClass)#%d (1) {
302        ["test"]=>
303        int(5)
304      }
305    }
306  }
307}
308