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===DONE===
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===DONE===
251