xref: /PHP-8.1/tests/lang/foreachLoop.016.phpt (revision f8d79582)
1--TEST--
2Ensure foreach splits the iterated entity from its cow reference set, for all sorts of iterated entities.
3--FILE--
4<?php
5
6  echo "\n" . '$a' . "\n";
7  $b = $a = array('original');
8  foreach($a as $k=>&$v) {
9     $v = 'changed';
10  }
11  var_dump($b);
12  unset($a, $b);
13
14  echo "\n" . '${\'a\'}' . "\n";
15  $b = $a = array('original');
16  foreach(${'a'} as $k=>&$v) {
17     $v = 'changed';
18  }
19  var_dump($b);
20  unset($a, $b);
21
22  echo "\n" . '$$a' . "\n";
23  $a = 'blah';
24  $$a = array('original');
25  $b = $$a;
26  foreach($$a as $k=>&$v) {
27     $v = 'changed';
28  }
29  var_dump($b);
30  unset($a, $b);
31
32  echo "\n" . '$a[0]' . "\n";
33  $b = $a[0] = array('original');
34  foreach($a[0] as $k=>&$v) {
35     $v = 'changed';
36  }
37  var_dump($b);
38  unset($a, $b);
39
40  echo "\n" . '$a[0][0]' . "\n";
41  $b = $a[0][0] = array('original');
42  foreach($a[0][0] as $k=>&$v) {
43     $v = 'changed';
44  }
45  var_dump($b);
46  unset($a, $b);
47
48  echo "\n" . '$a->b' . "\n";
49  $a = new stdClass;
50  $b = $a->b = array('original');
51  foreach($a->b as $k=>&$v) {
52     $v = 'changed';
53  }
54  var_dump($b);
55  unset($a, $b);
56
57  echo "\n" . '$a->b->c' . "\n";
58  $a = new stdClass;
59  $a->b = new stdClass;
60  $b = $a->b->c = array('original');
61  foreach($a->b as $k=>&$v) {
62     $v = 'changed';
63  }
64  var_dump($b);
65  unset($a, $b);
66
67  echo "\n" . '$a->b[0]' . "\n";
68  $a = new stdClass;
69  $b = $a->b[0] = array('original');
70  foreach($a->b[0] as $k=>&$v) {
71     $v = 'changed';
72  }
73  var_dump($b);
74  unset($a, $b);
75
76  echo "\n" . '$a->b[0][0]' . "\n";
77  $a = new stdClass;
78  $b = $a->b[0][0] = array('original');
79  foreach($a->b[0][0] as $k=>&$v) {
80     $v = 'changed';
81  }
82  var_dump($b);
83  unset($a, $b);
84
85  echo "\n" . '$a->b[0]->c' . "\n";
86  $a = new stdClass;
87  $a->b[0] = new stdClass;
88  $b = $a->b[0]->c = array('original');
89  foreach($a->b[0]->c as $k=>&$v) {
90     $v = 'changed';
91  }
92  var_dump($b);
93  unset($a, $b);
94
95  class C {
96    public static $a;
97  }
98
99  echo "\n" . 'C::$a' . "\n";
100  C::$a = array('original');
101  $b = C::$a;
102  foreach(C::$a as $k=>&$v) {
103     $v = 'changed';
104  }
105  var_dump($b);
106  unset($a, $b);
107
108  echo "\n" . 'C::$a[0]' . "\n";
109  C::$a[0] = array('original');
110  $b = C::$a[0];
111  foreach(C::$a[0] as $k=>&$v) {
112     $v = 'changed';
113  }
114  var_dump($b);
115  unset(C::$a[0], $b);
116
117  echo "\n" . 'C::$a[0]->b' . "\n";
118  C::$a[0] = new stdClass;
119  C::$a[0]->b = array('original');
120  $b = C::$a[0]->b;
121  foreach(C::$a[0]->b as $k=>&$v) {
122     $v = 'changed';
123  }
124  var_dump($b);
125  unset(C::$a[0]->b, $b);
126?>
127--EXPECT--
128$a
129array(1) {
130  [0]=>
131  string(8) "original"
132}
133
134${'a'}
135array(1) {
136  [0]=>
137  string(8) "original"
138}
139
140$$a
141array(1) {
142  [0]=>
143  string(8) "original"
144}
145
146$a[0]
147array(1) {
148  [0]=>
149  string(8) "original"
150}
151
152$a[0][0]
153array(1) {
154  [0]=>
155  string(8) "original"
156}
157
158$a->b
159array(1) {
160  [0]=>
161  string(8) "original"
162}
163
164$a->b->c
165array(1) {
166  [0]=>
167  string(8) "original"
168}
169
170$a->b[0]
171array(1) {
172  [0]=>
173  string(8) "original"
174}
175
176$a->b[0][0]
177array(1) {
178  [0]=>
179  string(8) "original"
180}
181
182$a->b[0]->c
183array(1) {
184  [0]=>
185  string(8) "original"
186}
187
188C::$a
189array(1) {
190  [0]=>
191  string(8) "original"
192}
193
194C::$a[0]
195array(1) {
196  [0]=>
197  string(8) "original"
198}
199
200C::$a[0]->b
201array(1) {
202  [0]=>
203  string(8) "original"
204}
205