1--TEST--
2Foreach loop tests - visibility.
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    function doForEachC() {
14        echo "in C::doForEachC\n";
15        foreach ($this as $k=>&$v) {
16            var_dump($v);
17            $v="changed.$k";
18        }
19    }
20
21    static function doForEach($obj) {
22        echo "in C::doForEach\n";
23        foreach ($obj as $k=>&$v) {
24            var_dump($v);
25            $v="changed.$k";
26        }
27    }
28
29    function doForEachOnThis() {
30        echo "in C::doForEachOnThis\n";
31        foreach ($this as $k=>&$v) {
32            var_dump($v);
33            $v="changed.$k";
34        }
35    }
36
37}
38
39class D extends C {
40
41    private $f = "Original f";
42    protected $g = "Original g";
43
44    static function doForEach($obj) {
45        echo "in D::doForEach\n";
46        foreach ($obj as $k=>&$v) {
47            var_dump($v);
48            $v="changed.$k";
49        }
50    }
51
52    function doForEachOnThis() {
53        echo "in D::doForEachOnThis\n";
54        foreach ($this as $k=>&$v) {
55            var_dump($v);
56            $v="changed.$k";
57        }
58    }
59}
60
61class E extends D {
62    public $a = "Overridden a";
63    public $b = "Overridden b";
64    public $c = "Overridden c";
65    protected $d = "Overridden d";
66    private $e = "Overridden e";
67
68    static function doForEach($obj) {
69        echo "in E::doForEach\n";
70        foreach ($obj as $k=>&$v) {
71            var_dump($v);
72            $v="changed.$k";
73        }
74    }
75
76    function doForEachOnThis() {
77        echo "in E::doForEachOnThis\n";
78        foreach ($this as $k=>&$v) {
79            var_dump($v);
80            $v="changed.$k";
81        }
82    }
83}
84
85echo "\n\nIterate over various generations from within overridden methods:\n";
86echo "\n--> Using instance of C:\n";
87$myC = new C;
88$myC->doForEachOnThis();
89var_dump($myC);
90echo "\n--> Using instance of D:\n";
91$myD = new D;
92$myD->doForEachOnThis();
93var_dump($myD);
94echo "\n--> Using instance of E:\n";
95$myE = new E;
96$myE->doForEachOnThis();
97var_dump($myE);
98
99echo "\n\nIterate over various generations from within an inherited method:\n";
100echo "\n--> Using instance of C:\n";
101$myC = new C;
102$myC->doForEachC();
103var_dump($myC);
104echo "\n--> Using instance of D:\n";
105$myD = new D;
106$myD->doForEachC();
107var_dump($myD);
108echo "\n--> Using instance of E:\n";
109$myE = new E;
110$myE->doForEachC();
111var_dump($myE);
112
113echo "\n\nIterate over various generations from within an overridden static method:\n";
114echo "\n--> Using instance of C:\n";
115$myC = new C;
116C::doForEach($myC);
117var_dump($myC);
118$myC = new C;
119D::doForEach($myC);
120var_dump($myC);
121$myC = new C;
122E::doForEach($myC);
123var_dump($myC);
124echo "\n--> Using instance of D:\n";
125$myD = new D;
126C::doForEach($myD);
127var_dump($myD);
128$myD = new D;
129D::doForEach($myD);
130var_dump($myD);
131$myD = new D;
132E::doForEach($myD);
133var_dump($myD);
134echo "\n--> Using instance of E:\n";
135$myE = new E;
136C::doForEach($myE);
137var_dump($myE);
138$myE = new E;
139D::doForEach($myE);
140var_dump($myE);
141$myE = new E;
142E::doForEach($myE);
143var_dump($myE);
144
145
146echo "\n\nIterate over various generations from outside the object:\n";
147echo "\n--> Using instance of C:\n";
148$myC = new C;
149foreach ($myC as $k=>&$v) {
150    var_dump($v);
151    $v="changed.$k";
152}
153var_dump($myC);
154echo "\n--> Using instance of D:\n";
155$myD = new D;
156foreach ($myD as $k=>&$v) {
157    var_dump($v);
158    $v="changed.$k";
159}
160var_dump($myD);
161echo "\n--> Using instance of E:\n";
162$myE = new E;
163foreach ($myE as $k=>&$v) {
164    var_dump($v);
165    $v="changed.$k";
166}
167var_dump($myE);
168?>
169--EXPECTF--
170Iterate over various generations from within overridden methods:
171
172--> Using instance of C:
173in C::doForEachOnThis
174string(10) "Original a"
175string(10) "Original b"
176string(10) "Original c"
177string(10) "Original d"
178string(10) "Original e"
179object(C)#%d (5) {
180  ["a"]=>
181  string(9) "changed.a"
182  ["b"]=>
183  string(9) "changed.b"
184  ["c"]=>
185  string(9) "changed.c"
186  ["d":protected]=>
187  string(9) "changed.d"
188  ["e":"C":private]=>
189  string(9) "changed.e"
190}
191
192--> Using instance of D:
193in D::doForEachOnThis
194string(10) "Original a"
195string(10) "Original b"
196string(10) "Original c"
197string(10) "Original d"
198string(10) "Original f"
199string(10) "Original g"
200object(D)#%d (7) {
201  ["a"]=>
202  string(9) "changed.a"
203  ["b"]=>
204  string(9) "changed.b"
205  ["c"]=>
206  string(9) "changed.c"
207  ["d":protected]=>
208  string(9) "changed.d"
209  ["e":"C":private]=>
210  string(10) "Original e"
211  ["f":"D":private]=>
212  string(9) "changed.f"
213  ["g":protected]=>
214  string(9) "changed.g"
215}
216
217--> Using instance of E:
218in E::doForEachOnThis
219string(12) "Overridden a"
220string(12) "Overridden b"
221string(12) "Overridden c"
222string(12) "Overridden d"
223string(10) "Original g"
224string(12) "Overridden e"
225object(E)#%d (8) {
226  ["a"]=>
227  string(9) "changed.a"
228  ["b"]=>
229  string(9) "changed.b"
230  ["c"]=>
231  string(9) "changed.c"
232  ["d":protected]=>
233  string(9) "changed.d"
234  ["e":"C":private]=>
235  string(10) "Original e"
236  ["f":"D":private]=>
237  string(10) "Original f"
238  ["g":protected]=>
239  string(9) "changed.g"
240  ["e":"E":private]=>
241  string(9) "changed.e"
242}
243
244
245Iterate over various generations from within an inherited method:
246
247--> Using instance of C:
248in C::doForEachC
249string(10) "Original a"
250string(10) "Original b"
251string(10) "Original c"
252string(10) "Original d"
253string(10) "Original e"
254object(C)#%d (5) {
255  ["a"]=>
256  string(9) "changed.a"
257  ["b"]=>
258  string(9) "changed.b"
259  ["c"]=>
260  string(9) "changed.c"
261  ["d":protected]=>
262  string(9) "changed.d"
263  ["e":"C":private]=>
264  string(9) "changed.e"
265}
266
267--> Using instance of D:
268in C::doForEachC
269string(10) "Original a"
270string(10) "Original b"
271string(10) "Original c"
272string(10) "Original d"
273string(10) "Original e"
274string(10) "Original g"
275object(D)#%d (7) {
276  ["a"]=>
277  string(9) "changed.a"
278  ["b"]=>
279  string(9) "changed.b"
280  ["c"]=>
281  string(9) "changed.c"
282  ["d":protected]=>
283  string(9) "changed.d"
284  ["e":"C":private]=>
285  string(9) "changed.e"
286  ["f":"D":private]=>
287  string(10) "Original f"
288  ["g":protected]=>
289  string(9) "changed.g"
290}
291
292--> Using instance of E:
293in C::doForEachC
294string(12) "Overridden a"
295string(12) "Overridden b"
296string(12) "Overridden c"
297string(12) "Overridden d"
298string(10) "Original e"
299string(10) "Original g"
300object(E)#%d (8) {
301  ["a"]=>
302  string(9) "changed.a"
303  ["b"]=>
304  string(9) "changed.b"
305  ["c"]=>
306  string(9) "changed.c"
307  ["d":protected]=>
308  string(9) "changed.d"
309  ["e":"C":private]=>
310  string(9) "changed.e"
311  ["f":"D":private]=>
312  string(10) "Original f"
313  ["g":protected]=>
314  string(9) "changed.g"
315  ["e":"E":private]=>
316  string(12) "Overridden e"
317}
318
319
320Iterate over various generations from within an overridden static method:
321
322--> Using instance of C:
323in C::doForEach
324string(10) "Original a"
325string(10) "Original b"
326string(10) "Original c"
327string(10) "Original d"
328string(10) "Original e"
329object(C)#%d (5) {
330  ["a"]=>
331  string(9) "changed.a"
332  ["b"]=>
333  string(9) "changed.b"
334  ["c"]=>
335  string(9) "changed.c"
336  ["d":protected]=>
337  string(9) "changed.d"
338  ["e":"C":private]=>
339  string(9) "changed.e"
340}
341in D::doForEach
342string(10) "Original a"
343string(10) "Original b"
344string(10) "Original c"
345string(10) "Original d"
346object(C)#%d (5) {
347  ["a"]=>
348  string(9) "changed.a"
349  ["b"]=>
350  string(9) "changed.b"
351  ["c"]=>
352  string(9) "changed.c"
353  ["d":protected]=>
354  string(9) "changed.d"
355  ["e":"C":private]=>
356  string(10) "Original e"
357}
358in E::doForEach
359string(10) "Original a"
360string(10) "Original b"
361string(10) "Original c"
362string(10) "Original d"
363object(C)#%d (5) {
364  ["a"]=>
365  string(9) "changed.a"
366  ["b"]=>
367  string(9) "changed.b"
368  ["c"]=>
369  string(9) "changed.c"
370  ["d":protected]=>
371  string(9) "changed.d"
372  ["e":"C":private]=>
373  string(10) "Original e"
374}
375
376--> Using instance of D:
377in C::doForEach
378string(10) "Original a"
379string(10) "Original b"
380string(10) "Original c"
381string(10) "Original d"
382string(10) "Original e"
383string(10) "Original g"
384object(D)#%d (7) {
385  ["a"]=>
386  string(9) "changed.a"
387  ["b"]=>
388  string(9) "changed.b"
389  ["c"]=>
390  string(9) "changed.c"
391  ["d":protected]=>
392  string(9) "changed.d"
393  ["e":"C":private]=>
394  string(9) "changed.e"
395  ["f":"D":private]=>
396  string(10) "Original f"
397  ["g":protected]=>
398  string(9) "changed.g"
399}
400in D::doForEach
401string(10) "Original a"
402string(10) "Original b"
403string(10) "Original c"
404string(10) "Original d"
405string(10) "Original f"
406string(10) "Original g"
407object(D)#%d (7) {
408  ["a"]=>
409  string(9) "changed.a"
410  ["b"]=>
411  string(9) "changed.b"
412  ["c"]=>
413  string(9) "changed.c"
414  ["d":protected]=>
415  string(9) "changed.d"
416  ["e":"C":private]=>
417  string(10) "Original e"
418  ["f":"D":private]=>
419  string(9) "changed.f"
420  ["g":protected]=>
421  string(9) "changed.g"
422}
423in E::doForEach
424string(10) "Original a"
425string(10) "Original b"
426string(10) "Original c"
427string(10) "Original d"
428string(10) "Original g"
429object(D)#%d (7) {
430  ["a"]=>
431  string(9) "changed.a"
432  ["b"]=>
433  string(9) "changed.b"
434  ["c"]=>
435  string(9) "changed.c"
436  ["d":protected]=>
437  string(9) "changed.d"
438  ["e":"C":private]=>
439  string(10) "Original e"
440  ["f":"D":private]=>
441  string(10) "Original f"
442  ["g":protected]=>
443  string(9) "changed.g"
444}
445
446--> Using instance of E:
447in C::doForEach
448string(12) "Overridden a"
449string(12) "Overridden b"
450string(12) "Overridden c"
451string(12) "Overridden d"
452string(10) "Original e"
453string(10) "Original g"
454object(E)#%d (8) {
455  ["a"]=>
456  string(9) "changed.a"
457  ["b"]=>
458  string(9) "changed.b"
459  ["c"]=>
460  string(9) "changed.c"
461  ["d":protected]=>
462  string(9) "changed.d"
463  ["e":"C":private]=>
464  string(9) "changed.e"
465  ["f":"D":private]=>
466  string(10) "Original f"
467  ["g":protected]=>
468  string(9) "changed.g"
469  ["e":"E":private]=>
470  string(12) "Overridden e"
471}
472in D::doForEach
473string(12) "Overridden a"
474string(12) "Overridden b"
475string(12) "Overridden c"
476string(12) "Overridden d"
477string(10) "Original f"
478string(10) "Original g"
479object(E)#%d (8) {
480  ["a"]=>
481  string(9) "changed.a"
482  ["b"]=>
483  string(9) "changed.b"
484  ["c"]=>
485  string(9) "changed.c"
486  ["d":protected]=>
487  string(9) "changed.d"
488  ["e":"C":private]=>
489  string(10) "Original e"
490  ["f":"D":private]=>
491  string(9) "changed.f"
492  ["g":protected]=>
493  string(9) "changed.g"
494  ["e":"E":private]=>
495  string(12) "Overridden e"
496}
497in E::doForEach
498string(12) "Overridden a"
499string(12) "Overridden b"
500string(12) "Overridden c"
501string(12) "Overridden d"
502string(10) "Original g"
503string(12) "Overridden e"
504object(E)#%d (8) {
505  ["a"]=>
506  string(9) "changed.a"
507  ["b"]=>
508  string(9) "changed.b"
509  ["c"]=>
510  string(9) "changed.c"
511  ["d":protected]=>
512  string(9) "changed.d"
513  ["e":"C":private]=>
514  string(10) "Original e"
515  ["f":"D":private]=>
516  string(10) "Original f"
517  ["g":protected]=>
518  string(9) "changed.g"
519  ["e":"E":private]=>
520  string(9) "changed.e"
521}
522
523
524Iterate over various generations from outside the object:
525
526--> Using instance of C:
527string(10) "Original a"
528string(10) "Original b"
529string(10) "Original c"
530object(C)#%d (5) {
531  ["a"]=>
532  string(9) "changed.a"
533  ["b"]=>
534  string(9) "changed.b"
535  ["c"]=>
536  &string(9) "changed.c"
537  ["d":protected]=>
538  string(10) "Original d"
539  ["e":"C":private]=>
540  string(10) "Original e"
541}
542
543--> Using instance of D:
544string(10) "Original a"
545string(10) "Original b"
546string(10) "Original c"
547object(D)#%d (7) {
548  ["a"]=>
549  string(9) "changed.a"
550  ["b"]=>
551  string(9) "changed.b"
552  ["c"]=>
553  &string(9) "changed.c"
554  ["d":protected]=>
555  string(10) "Original d"
556  ["e":"C":private]=>
557  string(10) "Original e"
558  ["f":"D":private]=>
559  string(10) "Original f"
560  ["g":protected]=>
561  string(10) "Original g"
562}
563
564--> Using instance of E:
565string(12) "Overridden a"
566string(12) "Overridden b"
567string(12) "Overridden c"
568object(E)#%d (8) {
569  ["a"]=>
570  string(9) "changed.a"
571  ["b"]=>
572  string(9) "changed.b"
573  ["c"]=>
574  &string(9) "changed.c"
575  ["d":protected]=>
576  string(12) "Overridden d"
577  ["e":"C":private]=>
578  string(10) "Original e"
579  ["f":"D":private]=>
580  string(10) "Original f"
581  ["g":protected]=>
582  string(10) "Original g"
583  ["e":"E":private]=>
584  string(12) "Overridden e"
585}
586