1--TEST-- 2Foreach loop tests - modifying the object during the loop. 3--FILE-- 4<?php 5 6class C { 7 public $a = "Original a"; 8 public $b = "Original b"; 9 public $c = "Original c"; 10 protected $d = "Original d"; 11 private $e = "Original e"; 12} 13 14echo "\nDirectly changing object values.\n"; 15$obj = new C; 16foreach ($obj as $k=>$v) { 17 $obj->$k="changed.$k"; 18 var_dump($v); 19} 20var_dump($obj); 21 22echo "\nModifying the foreach \$value.\n"; 23$obj = new C; 24foreach ($obj as $k=>$v) { 25 $v="changed.$k"; 26} 27var_dump($obj); 28 29 30echo "\nModifying the foreach &\$value.\n"; 31$obj = new C; 32foreach ($obj as $k=>&$v) { 33 $v="changed.$k"; 34} 35var_dump($obj); 36 37echo "\nAdding properties to an an object.\n"; 38$obj = new C; 39$counter=0; 40foreach ($obj as $v) { 41 $newPropName = "new$counter"; 42 $obj->$newPropName = "Added property $counter"; 43 if ($counter++>10) { 44 echo "Loop detected\n"; 45 break; 46 } 47 var_dump($v); 48} 49var_dump($obj); 50 51echo "\nAdding properties to an an object, using &\$value.\n"; 52$obj = new C; 53$counter=0; 54foreach ($obj as &$v) { 55 $newPropName = "new$counter"; 56 $obj->$newPropName = "Added property $counter"; 57 if ($counter++>10) { 58 echo "Loop detected\n"; 59 break; 60 } 61 var_dump($v); 62} 63var_dump($obj); 64 65echo "\nRemoving properties from an object.\n"; 66$obj = new C; 67foreach ($obj as $v) { 68 unset($obj->a); 69 unset($obj->b); 70 unset($obj->c); 71 var_dump($v); 72} 73var_dump($obj); 74 75echo "\nRemoving properties from an object, using &\$value.\n"; 76$obj = new C; 77foreach ($obj as &$v) { 78 unset($obj->a); 79 unset($obj->b); 80 unset($obj->c); 81 var_dump($v); 82} 83var_dump($obj); 84 85?> 86--EXPECTF-- 87Directly changing object values. 88string(10) "Original a" 89string(10) "Original b" 90string(10) "Original c" 91object(C)#%d (5) { 92 ["a"]=> 93 string(9) "changed.a" 94 ["b"]=> 95 string(9) "changed.b" 96 ["c"]=> 97 string(9) "changed.c" 98 ["d":protected]=> 99 string(10) "Original d" 100 ["e":"C":private]=> 101 string(10) "Original e" 102} 103 104Modifying the foreach $value. 105object(C)#%d (5) { 106 ["a"]=> 107 string(10) "Original a" 108 ["b"]=> 109 string(10) "Original b" 110 ["c"]=> 111 string(10) "Original c" 112 ["d":protected]=> 113 string(10) "Original d" 114 ["e":"C":private]=> 115 string(10) "Original e" 116} 117 118Modifying the foreach &$value. 119object(C)#%d (5) { 120 ["a"]=> 121 string(9) "changed.a" 122 ["b"]=> 123 string(9) "changed.b" 124 ["c"]=> 125 &string(9) "changed.c" 126 ["d":protected]=> 127 string(10) "Original d" 128 ["e":"C":private]=> 129 string(10) "Original e" 130} 131 132Adding properties to an an object. 133string(10) "Original a" 134string(10) "Original b" 135string(10) "Original c" 136string(16) "Added property 0" 137string(16) "Added property 1" 138string(16) "Added property 2" 139string(16) "Added property 3" 140string(16) "Added property 4" 141string(16) "Added property 5" 142string(16) "Added property 6" 143string(16) "Added property 7" 144Loop detected 145object(C)#%d (17) { 146 ["a"]=> 147 string(10) "Original a" 148 ["b"]=> 149 string(10) "Original b" 150 ["c"]=> 151 string(10) "Original c" 152 ["d":protected]=> 153 string(10) "Original d" 154 ["e":"C":private]=> 155 string(10) "Original e" 156 ["new0"]=> 157 string(16) "Added property 0" 158 ["new1"]=> 159 string(16) "Added property 1" 160 ["new2"]=> 161 string(16) "Added property 2" 162 ["new3"]=> 163 string(16) "Added property 3" 164 ["new4"]=> 165 string(16) "Added property 4" 166 ["new5"]=> 167 string(16) "Added property 5" 168 ["new6"]=> 169 string(16) "Added property 6" 170 ["new7"]=> 171 string(16) "Added property 7" 172 ["new8"]=> 173 string(16) "Added property 8" 174 ["new9"]=> 175 string(16) "Added property 9" 176 ["new10"]=> 177 string(17) "Added property 10" 178 ["new11"]=> 179 string(17) "Added property 11" 180} 181 182Adding properties to an an object, using &$value. 183string(10) "Original a" 184string(10) "Original b" 185string(10) "Original c" 186string(16) "Added property 0" 187string(16) "Added property 1" 188string(16) "Added property 2" 189string(16) "Added property 3" 190string(16) "Added property 4" 191string(16) "Added property 5" 192string(16) "Added property 6" 193string(16) "Added property 7" 194Loop detected 195object(C)#%d (17) { 196 ["a"]=> 197 string(10) "Original a" 198 ["b"]=> 199 string(10) "Original b" 200 ["c"]=> 201 string(10) "Original c" 202 ["d":protected]=> 203 string(10) "Original d" 204 ["e":"C":private]=> 205 string(10) "Original e" 206 ["new0"]=> 207 string(16) "Added property 0" 208 ["new1"]=> 209 string(16) "Added property 1" 210 ["new2"]=> 211 string(16) "Added property 2" 212 ["new3"]=> 213 string(16) "Added property 3" 214 ["new4"]=> 215 string(16) "Added property 4" 216 ["new5"]=> 217 string(16) "Added property 5" 218 ["new6"]=> 219 string(16) "Added property 6" 220 ["new7"]=> 221 string(16) "Added property 7" 222 ["new8"]=> 223 &string(16) "Added property 8" 224 ["new9"]=> 225 string(16) "Added property 9" 226 ["new10"]=> 227 string(17) "Added property 10" 228 ["new11"]=> 229 string(17) "Added property 11" 230} 231 232Removing properties from an object. 233string(10) "Original a" 234object(C)#%d (2) { 235 ["d":protected]=> 236 string(10) "Original d" 237 ["e":"C":private]=> 238 string(10) "Original e" 239} 240 241Removing properties from an object, using &$value. 242string(10) "Original a" 243object(C)#%d (2) { 244 ["d":protected]=> 245 string(10) "Original d" 246 ["e":"C":private]=> 247 string(10) "Original e" 248} 249