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-- 87 88 89Array containing same object twice: 90array(2) { 91 [0]=> 92 object(stdClass)#%d (0) { 93 } 94 [1]=> 95 object(stdClass)#%d (0) { 96 } 97} 98string(37) "a:2:{i:0;O:8:"stdClass":0:{}i:1;r:2;}" 99array(2) { 100 [0]=> 101 object(stdClass)#%d (0) { 102 } 103 [1]=> 104 object(stdClass)#%d (0) { 105 } 106} 107array(2) { 108 [0]=> 109 object(stdClass)#%d (1) { 110 ["a"]=> 111 string(7) "newProp" 112 } 113 [1]=> 114 object(stdClass)#%d (1) { 115 ["a"]=> 116 string(7) "newProp" 117 } 118} 119array(2) { 120 [0]=> 121 string(10) "a0.changed" 122 [1]=> 123 object(stdClass)#%d (1) { 124 ["a"]=> 125 string(7) "newProp" 126 } 127} 128 129 130Array containing object and reference to that object: 131array(2) { 132 [0]=> 133 &object(stdClass)#%d (0) { 134 } 135 [1]=> 136 &object(stdClass)#%d (0) { 137 } 138} 139string(37) "a:2:{i:0;O:8:"stdClass":0:{}i:1;R:2;}" 140array(2) { 141 [0]=> 142 &object(stdClass)#%d (0) { 143 } 144 [1]=> 145 &object(stdClass)#%d (0) { 146 } 147} 148array(2) { 149 [0]=> 150 &object(stdClass)#%d (1) { 151 ["a"]=> 152 string(7) "newProp" 153 } 154 [1]=> 155 &object(stdClass)#%d (1) { 156 ["a"]=> 157 string(7) "newProp" 158 } 159} 160array(2) { 161 [0]=> 162 &string(10) "a0.changed" 163 [1]=> 164 &string(10) "a0.changed" 165} 166 167 168Object containing same object twice:object(stdClass)#%d (2) { 169 ["a"]=> 170 object(stdClass)#%d (0) { 171 } 172 ["b"]=> 173 object(stdClass)#%d (0) { 174 } 175} 176string(58) "O:8:"stdClass":2:{s:1:"a";O:8:"stdClass":0:{}s:1:"b";r:2;}" 177object(stdClass)#%d (2) { 178 ["a"]=> 179 object(stdClass)#%d (0) { 180 } 181 ["b"]=> 182 object(stdClass)#%d (0) { 183 } 184} 185object(stdClass)#%d (2) { 186 ["a"]=> 187 object(stdClass)#%d (1) { 188 ["a"]=> 189 string(7) "newProp" 190 } 191 ["b"]=> 192 object(stdClass)#%d (1) { 193 ["a"]=> 194 string(7) "newProp" 195 } 196} 197object(stdClass)#%d (2) { 198 ["a"]=> 199 string(20) "container->a.changed" 200 ["b"]=> 201 object(stdClass)#%d (1) { 202 ["a"]=> 203 string(7) "newProp" 204 } 205} 206 207 208Object containing object and reference to that object: 209object(stdClass)#%d (2) { 210 ["a"]=> 211 &object(stdClass)#%d (0) { 212 } 213 ["b"]=> 214 &object(stdClass)#%d (0) { 215 } 216} 217string(58) "O:8:"stdClass":2:{s:1:"a";O:8:"stdClass":0:{}s:1:"b";R:2;}" 218object(stdClass)#%d (2) { 219 ["a"]=> 220 &object(stdClass)#%d (0) { 221 } 222 ["b"]=> 223 &object(stdClass)#%d (0) { 224 } 225} 226object(stdClass)#%d (2) { 227 ["a"]=> 228 &object(stdClass)#%d (1) { 229 ["a"]=> 230 string(7) "newProp" 231 } 232 ["b"]=> 233 &object(stdClass)#%d (1) { 234 ["a"]=> 235 string(7) "newProp" 236 } 237} 238object(stdClass)#%d (2) { 239 ["a"]=> 240 &string(20) "container->a.changed" 241 ["b"]=> 242 &string(20) "container->a.changed" 243} 244Done