xref: /PHP-8.4/ext/spl/tests/observer_005.phpt (revision 11648c00)
1--TEST--
2SPL: SplObjectStorage serialization & visibility
3--FILE--
4<?php
5
6class TestClass
7{
8    public    $def = 24;
9    public    $pub = 25;
10    protected $pro = 26;
11    private   $pri = 27;
12
13    public function __construct($pub = 42, $pro = 43, $pri = 44)
14    {
15        $this->pub = $pub;
16        $this->pro = $pro;
17        $this->pri = $pri;
18    }
19}
20
21class ExtTestClass
22{
23}
24
25class MyStorage extends SplObjectStorage
26{
27    public    $def = 24;
28    public    $pub = 25;
29    protected $pro = 26;
30    private   $pri = 27;
31
32    public function __construct($pub = 52, $pro = 53, $pri = 54)
33    {
34        $this->pub = $pub;
35        $this->pro = $pro;
36        $this->pri = $pri;
37    }
38}
39
40class ExtStorage extends MyStorage
41{
42}
43
44$storage = new MyStorage(1,2,3);
45
46foreach(array(array(4,5,6),array(7,8,9)) as $value)
47{
48     $storage->attach(new TestClass($value[0], $value[1], $value[2]));
49}
50
51var_dump(count($storage));
52
53foreach($storage as $object)
54{
55    var_dump($object);
56}
57
58var_dump($storage);
59
60var_dump(serialize($storage));
61echo "===UNSERIALIZE===\n";
62
63$storage2 = unserialize(serialize($storage));
64
65var_dump(count($storage2));
66
67foreach($storage2 as $object)
68{
69    var_dump($object);
70}
71
72var_dump($storage2);
73
74?>
75--EXPECTF--
76int(2)
77object(TestClass)#%d (4) {
78  ["def"]=>
79  int(24)
80  ["pub"]=>
81  int(4)
82  ["pro":protected]=>
83  int(5)
84  ["pri":"TestClass":private]=>
85  int(6)
86}
87object(TestClass)#%d (4) {
88  ["def"]=>
89  int(24)
90  ["pub"]=>
91  int(7)
92  ["pro":protected]=>
93  int(8)
94  ["pri":"TestClass":private]=>
95  int(9)
96}
97object(MyStorage)#%d (5) {
98  ["def"]=>
99  int(24)
100  ["pub"]=>
101  int(1)
102  ["pro":protected]=>
103  int(2)
104  ["pri":"MyStorage":private]=>
105  int(3)
106  ["storage":"SplObjectStorage":private]=>
107  array(2) {
108    [0]=>
109    array(2) {
110      ["obj"]=>
111      object(TestClass)#%d (4) {
112        ["def"]=>
113        int(24)
114        ["pub"]=>
115        int(4)
116        ["pro":protected]=>
117        int(5)
118        ["pri":"TestClass":private]=>
119        int(6)
120      }
121      ["inf"]=>
122      NULL
123    }
124    [1]=>
125    array(2) {
126      ["obj"]=>
127      object(TestClass)#%d (4) {
128        ["def"]=>
129        int(24)
130        ["pub"]=>
131        int(7)
132        ["pro":protected]=>
133        int(8)
134        ["pri":"TestClass":private]=>
135        int(9)
136      }
137      ["inf"]=>
138      NULL
139    }
140  }
141}
142string(%d) "%s"
143===UNSERIALIZE===
144int(2)
145object(TestClass)#%d (4) {
146  ["def"]=>
147  int(24)
148  ["pub"]=>
149  int(4)
150  ["pro":protected]=>
151  int(5)
152  ["pri":"TestClass":private]=>
153  int(6)
154}
155object(TestClass)#%d (4) {
156  ["def"]=>
157  int(24)
158  ["pub"]=>
159  int(7)
160  ["pro":protected]=>
161  int(8)
162  ["pri":"TestClass":private]=>
163  int(9)
164}
165object(MyStorage)#%d (5) {
166  ["def"]=>
167  int(24)
168  ["pub"]=>
169  int(1)
170  ["pro":protected]=>
171  int(2)
172  ["pri":"MyStorage":private]=>
173  int(3)
174  ["storage":"SplObjectStorage":private]=>
175  array(2) {
176    [0]=>
177    array(2) {
178      ["obj"]=>
179      object(TestClass)#%d (4) {
180        ["def"]=>
181        int(24)
182        ["pub"]=>
183        int(4)
184        ["pro":protected]=>
185        int(5)
186        ["pri":"TestClass":private]=>
187        int(6)
188      }
189      ["inf"]=>
190      NULL
191    }
192    [1]=>
193    array(2) {
194      ["obj"]=>
195      object(TestClass)#%d (4) {
196        ["def"]=>
197        int(24)
198        ["pub"]=>
199        int(7)
200        ["pro":protected]=>
201        int(8)
202        ["pri":"TestClass":private]=>
203        int(9)
204      }
205      ["inf"]=>
206      NULL
207    }
208  }
209}
210