1--TEST--
2Foreach loop tests - substituting the entire iterated entity 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    public $d = "Original d";
11    public $e = "Original e";
12}
13
14echo "\nSubstituting the iterated object for a different object.\n";
15$obj = new C;
16$obj2 = new stdclass;
17$obj2->a = "new a";
18$obj2->b = "new b";
19$obj2->c = "new c";
20$obj2->d = "new d";
21$obj2->e = "new e";
22$obj2->f = "new f";
23$ref = &$obj;
24$count=0;
25foreach ($obj as $v) {
26    var_dump($v);
27    if ($v==$obj->b) {
28      $ref=$obj2;
29    }
30    if (++$count>10) {
31        echo "Loop detected.\n";
32        break;
33    }
34}
35var_dump($obj);
36
37echo "\nSubstituting the iterated object for an array.\n";
38$obj = new C;
39$a = array(1,2,3,4,5,6,7,8);
40$ref = &$obj;
41$count=0;
42foreach ($obj as $v) {
43    var_dump($v);
44    if ($v==="Original b") {
45      $ref=$a;
46    }
47    if (++$count>10) {
48        echo "Loop detected.\n";
49        break;
50    }
51}
52var_dump($obj);
53
54echo "\nSubstituting the iterated array for an object.\n";
55$a = array(1,2,3,4,5,6,7,8);
56$obj = new C;
57$ref = &$a;
58$count=0;
59foreach ($a as $v) {
60    var_dump($v);
61    if ($v===2) {
62      $ref=$obj;
63    }
64    if (++$count>10) {
65        echo "Loop detected.\n";
66        break;
67    }
68}
69var_dump($obj);
70
71?>
72--EXPECTF--
73Substituting the iterated object for a different object.
74string(10) "Original a"
75string(10) "Original b"
76string(10) "Original c"
77string(10) "Original d"
78string(10) "Original e"
79object(stdClass)#%d (6) {
80  ["a"]=>
81  string(5) "new a"
82  ["b"]=>
83  string(5) "new b"
84  ["c"]=>
85  string(5) "new c"
86  ["d"]=>
87  string(5) "new d"
88  ["e"]=>
89  string(5) "new e"
90  ["f"]=>
91  string(5) "new f"
92}
93
94Substituting the iterated object for an array.
95string(10) "Original a"
96string(10) "Original b"
97string(10) "Original c"
98string(10) "Original d"
99string(10) "Original e"
100array(8) {
101  [0]=>
102  int(1)
103  [1]=>
104  int(2)
105  [2]=>
106  int(3)
107  [3]=>
108  int(4)
109  [4]=>
110  int(5)
111  [5]=>
112  int(6)
113  [6]=>
114  int(7)
115  [7]=>
116  int(8)
117}
118
119Substituting the iterated array for an object.
120int(1)
121int(2)
122int(3)
123int(4)
124int(5)
125int(6)
126int(7)
127int(8)
128object(C)#%d (5) {
129  ["a"]=>
130  string(10) "Original a"
131  ["b"]=>
132  string(10) "Original b"
133  ["c"]=>
134  string(10) "Original c"
135  ["d"]=>
136  string(10) "Original d"
137  ["e"]=>
138  string(10) "Original e"
139}
140