xref: /PHP-8.0/ext/spl/tests/observer_004.phpt (revision f8d79582)
1--TEST--
2SPL: SplObjectStorage serialization & overloading
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,2) as $value)
29{
30     $storage->attach(new TestClass($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
56?>
57--EXPECTF--
58int(2)
59int(1)
60int(2)
61object(MyStorage)#%d (2) {
62  ["bla"]=>
63  int(26)
64  ["storage":"SplObjectStorage":private]=>
65  array(2) {
66    ["%s"]=>
67    array(2) {
68      ["obj"]=>
69      object(TestClass)#%d (1) {
70        ["test"]=>
71        int(1)
72      }
73      ["inf"]=>
74      NULL
75    }
76    ["%s"]=>
77    array(2) {
78      ["obj"]=>
79      object(TestClass)#%d (1) {
80        ["test"]=>
81        int(2)
82      }
83      ["inf"]=>
84      NULL
85    }
86  }
87}
88string(%d) "%s"
89===UNSERIALIZE===
90int(2)
91int(1)
92int(2)
93object(MyStorage)#%d (2) {
94  ["bla"]=>
95  int(26)
96  ["storage":"SplObjectStorage":private]=>
97  array(2) {
98    ["%s"]=>
99    array(2) {
100      ["obj"]=>
101      object(TestClass)#%d (1) {
102        ["test"]=>
103        int(1)
104      }
105      ["inf"]=>
106      NULL
107    }
108    ["%s"]=>
109    array(2) {
110      ["obj"]=>
111      object(TestClass)#%d (1) {
112        ["test"]=>
113        int(2)
114      }
115      ["inf"]=>
116      NULL
117    }
118  }
119}
120