1--TEST--
2Object serialization / unserialization: real references and COW references
3--FILE--
4<?php
5/* Prototype  : proto string serialize(mixed variable)
6 * Description: Returns a string representation of variable (which can later be unserialized)
7 * Source code: ext/standard/var.c
8 * Alias to functions:
9 */
10/* Prototype  : proto mixed unserialize(string variable_representation)
11 * Description: Takes a string representation of variable and recreates it
12 * Source code: ext/standard/var.c
13 * Alias to functions:
14 */
15
16echo "\n\nArray containing same object twice:\n";
17$obj = new stdclass;
18$a[0] = $obj;
19$a[1] = $a[0];
20var_dump($a);
21
22$ser = serialize($a);
23var_dump($ser);
24
25$ua = unserialize($ser);
26var_dump($ua);
27$ua[0]->a = "newProp";
28var_dump($ua);
29$ua[0] = "a0.changed";
30var_dump($ua);
31
32
33echo "\n\nArray containing object and reference to that object:\n";
34$obj = new stdclass;
35$a[0] = $obj;
36$a[1] = &$a[0];
37var_dump($a);
38
39$ser = serialize($a);
40var_dump($ser);
41
42$ua = unserialize($ser);
43var_dump($ua);
44$ua[0]->a = "newProp";
45var_dump($ua);
46$ua[0] = "a0.changed";
47var_dump($ua);
48
49echo "\n\nObject containing same object twice:";
50$obj = new stdclass;
51$contaner = new stdclass;
52$contaner->a = $obj;
53$contaner->b = $contaner->a;
54var_dump($contaner);
55
56$ser = serialize($contaner);
57var_dump($ser);
58
59$ucontainer = unserialize($ser);
60var_dump($ucontainer);
61$ucontainer->a->a = "newProp";
62var_dump($ucontainer);
63$ucontainer->a = "container->a.changed";
64var_dump($ucontainer);
65
66
67echo "\n\nObject containing object and reference to that object:\n";
68$obj = new stdclass;
69$contaner = new stdclass;
70$contaner->a = $obj;
71$contaner->b = &$contaner->a;
72var_dump($contaner);
73
74$ser = serialize($contaner);
75var_dump($ser);
76
77$ucontainer = unserialize($ser);
78var_dump($ucontainer);
79$ucontainer->a->a = "newProp";
80var_dump($ucontainer);
81$ucontainer->b = "container->a.changed";
82var_dump($ucontainer);
83
84echo "Done";
85?>
86--EXPECTF--
87Array containing same object twice:
88array(2) {
89  [0]=>
90  object(stdClass)#%d (0) {
91  }
92  [1]=>
93  object(stdClass)#%d (0) {
94  }
95}
96string(37) "a:2:{i:0;O:8:"stdClass":0:{}i:1;r:2;}"
97array(2) {
98  [0]=>
99  object(stdClass)#%d (0) {
100  }
101  [1]=>
102  object(stdClass)#%d (0) {
103  }
104}
105array(2) {
106  [0]=>
107  object(stdClass)#%d (1) {
108    ["a"]=>
109    string(7) "newProp"
110  }
111  [1]=>
112  object(stdClass)#%d (1) {
113    ["a"]=>
114    string(7) "newProp"
115  }
116}
117array(2) {
118  [0]=>
119  string(10) "a0.changed"
120  [1]=>
121  object(stdClass)#%d (1) {
122    ["a"]=>
123    string(7) "newProp"
124  }
125}
126
127
128Array containing object and reference to that object:
129array(2) {
130  [0]=>
131  &object(stdClass)#%d (0) {
132  }
133  [1]=>
134  &object(stdClass)#%d (0) {
135  }
136}
137string(37) "a:2:{i:0;O:8:"stdClass":0:{}i:1;R:2;}"
138array(2) {
139  [0]=>
140  &object(stdClass)#%d (0) {
141  }
142  [1]=>
143  &object(stdClass)#%d (0) {
144  }
145}
146array(2) {
147  [0]=>
148  &object(stdClass)#%d (1) {
149    ["a"]=>
150    string(7) "newProp"
151  }
152  [1]=>
153  &object(stdClass)#%d (1) {
154    ["a"]=>
155    string(7) "newProp"
156  }
157}
158array(2) {
159  [0]=>
160  &string(10) "a0.changed"
161  [1]=>
162  &string(10) "a0.changed"
163}
164
165
166Object containing same object twice:object(stdClass)#%d (2) {
167  ["a"]=>
168  object(stdClass)#%d (0) {
169  }
170  ["b"]=>
171  object(stdClass)#%d (0) {
172  }
173}
174string(58) "O:8:"stdClass":2:{s:1:"a";O:8:"stdClass":0:{}s:1:"b";r:2;}"
175object(stdClass)#%d (2) {
176  ["a"]=>
177  object(stdClass)#%d (0) {
178  }
179  ["b"]=>
180  object(stdClass)#%d (0) {
181  }
182}
183object(stdClass)#%d (2) {
184  ["a"]=>
185  object(stdClass)#%d (1) {
186    ["a"]=>
187    string(7) "newProp"
188  }
189  ["b"]=>
190  object(stdClass)#%d (1) {
191    ["a"]=>
192    string(7) "newProp"
193  }
194}
195object(stdClass)#%d (2) {
196  ["a"]=>
197  string(20) "container->a.changed"
198  ["b"]=>
199  object(stdClass)#%d (1) {
200    ["a"]=>
201    string(7) "newProp"
202  }
203}
204
205
206Object containing object and reference to that object:
207object(stdClass)#%d (2) {
208  ["a"]=>
209  &object(stdClass)#%d (0) {
210  }
211  ["b"]=>
212  &object(stdClass)#%d (0) {
213  }
214}
215string(58) "O:8:"stdClass":2:{s:1:"a";O:8:"stdClass":0:{}s:1:"b";R:2;}"
216object(stdClass)#%d (2) {
217  ["a"]=>
218  &object(stdClass)#%d (0) {
219  }
220  ["b"]=>
221  &object(stdClass)#%d (0) {
222  }
223}
224object(stdClass)#%d (2) {
225  ["a"]=>
226  &object(stdClass)#%d (1) {
227    ["a"]=>
228    string(7) "newProp"
229  }
230  ["b"]=>
231  &object(stdClass)#%d (1) {
232    ["a"]=>
233    string(7) "newProp"
234  }
235}
236object(stdClass)#%d (2) {
237  ["a"]=>
238  &string(20) "container->a.changed"
239  ["b"]=>
240  &string(20) "container->a.changed"
241}
242Done
243