xref: /PHP-7.4/ext/spl/tests/observer_006.phpt (revision 782352c5)
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===DONE===
79<?php exit(0); ?>
80--EXPECTF--
81int(2)
82int(1)
83int(2)
84object(MyStorage)#%d (2) {
85  ["bla"]=>
86  int(26)
87  ["storage":"SplObjectStorage":private]=>
88  array(2) {
89    ["%s"]=>
90    array(2) {
91      ["obj"]=>
92      object(TestClass)#%d (1) {
93        ["test"]=>
94        int(1)
95      }
96      ["inf"]=>
97      string(3) "foo"
98    }
99    ["%s"]=>
100    array(2) {
101      ["obj"]=>
102      object(TestClass)#%d (1) {
103        ["test"]=>
104        int(2)
105      }
106      ["inf"]=>
107      int(42)
108    }
109  }
110}
111string(%d) "%s"
112===UNSERIALIZE===
113int(2)
114int(1)
115int(2)
116object(MyStorage)#%d (2) {
117  ["bla"]=>
118  int(26)
119  ["storage":"SplObjectStorage":private]=>
120  array(2) {
121    ["%s"]=>
122    array(2) {
123      ["obj"]=>
124      object(TestClass)#%d (1) {
125        ["test"]=>
126        int(1)
127      }
128      ["inf"]=>
129      string(3) "foo"
130    }
131    ["%s"]=>
132    array(2) {
133      ["obj"]=>
134      object(TestClass)#%d (1) {
135        ["test"]=>
136        int(2)
137      }
138      ["inf"]=>
139      int(42)
140    }
141  }
142}
143===UNSERIALIZE2===
144object(MyStorage)#%d (2) {
145  ["bla"]=>
146  int(26)
147  ["storage":"SplObjectStorage":private]=>
148  array(4) {
149    ["%s"]=>
150    array(2) {
151      ["obj"]=>
152      object(TestClass)#%d (1) {
153        ["test"]=>
154        int(1)
155      }
156      ["inf"]=>
157      string(3) "foo"
158    }
159    ["%s"]=>
160    array(2) {
161      ["obj"]=>
162      object(TestClass)#%d (1) {
163        ["test"]=>
164        int(2)
165      }
166      ["inf"]=>
167      int(42)
168    }
169    ["%s"]=>
170    array(2) {
171      ["obj"]=>
172      object(TestClass)#%d (1) {
173        ["test"]=>
174        int(3)
175      }
176      ["inf"]=>
177      object(stdClass)#%d (0) {
178      }
179    }
180    ["%s"]=>
181    array(2) {
182      ["obj"]=>
183      object(TestClass)#%d (1) {
184        ["test"]=>
185        int(4)
186      }
187      ["inf"]=>
188      object(TestClass)#%d (1) {
189        ["test"]=>
190        int(5)
191      }
192    }
193  }
194}
195int(1)
196object(TestClass)#%d (1) {
197  ["test"]=>
198  int(2)
199}
200int(42)
201string(3) "bar"
202===UNSERIALIZE3===
203object(MyStorage)#%d (2) {
204  ["bla"]=>
205  int(26)
206  ["storage":"SplObjectStorage":private]=>
207  array(4) {
208    ["%s"]=>
209    array(2) {
210      ["obj"]=>
211      object(TestClass)#%d (1) {
212        ["test"]=>
213        int(1)
214      }
215      ["inf"]=>
216      string(3) "foo"
217    }
218    ["%s"]=>
219    array(2) {
220      ["obj"]=>
221      object(TestClass)#%d (1) {
222        ["test"]=>
223        int(2)
224      }
225      ["inf"]=>
226      string(3) "bar"
227    }
228    ["%s"]=>
229    array(2) {
230      ["obj"]=>
231      object(TestClass)#%d (1) {
232        ["test"]=>
233        int(3)
234      }
235      ["inf"]=>
236      object(stdClass)#%d (0) {
237      }
238    }
239    ["%s"]=>
240    array(2) {
241      ["obj"]=>
242      object(TestClass)#%d (1) {
243        ["test"]=>
244        int(4)
245      }
246      ["inf"]=>
247      object(TestClass)#%d (1) {
248        ["test"]=>
249        int(5)
250      }
251    }
252  }
253}
254int(2)
255object(TestClass)#7 (1) {
256  ["test"]=>
257  int(3)
258}
259===UNSERIALIZE4===
260object(MyStorage)#%d (2) {
261  ["bla"]=>
262  int(26)
263  ["storage":"SplObjectStorage":private]=>
264  array(4) {
265    ["%s"]=>
266    array(2) {
267      ["obj"]=>
268      object(TestClass)#%d (1) {
269        ["test"]=>
270        int(1)
271      }
272      ["inf"]=>
273      string(3) "foo"
274    }
275    ["%s"]=>
276    array(2) {
277      ["obj"]=>
278      object(TestClass)#%d (1) {
279        ["test"]=>
280        int(2)
281      }
282      ["inf"]=>
283      string(3) "bar"
284    }
285    ["%s"]=>
286    array(2) {
287      ["obj"]=>
288      object(TestClass)#%d (1) {
289        ["test"]=>
290        int(3)
291      }
292      ["inf"]=>
293      string(8) "replaced"
294    }
295    ["%s"]=>
296    array(2) {
297      ["obj"]=>
298      object(TestClass)#%d (1) {
299        ["test"]=>
300        int(4)
301      }
302      ["inf"]=>
303      object(TestClass)#%d (1) {
304        ["test"]=>
305        int(5)
306      }
307    }
308  }
309}
310===DONE===
311