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