xref: /PHP-7.4/tests/lang/foreachLoop.016.phpt (revision e63febb1)
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  $b = $a->b = array('original');
50  foreach($a->b as $k=>&$v) {
51  	 $v = 'changed';
52  }
53  var_dump($b);
54  unset($a, $b);
55
56  echo "\n" . '$a->b->c' . "\n";
57  $b = $a->b->c = array('original');
58  foreach($a->b as $k=>&$v) {
59  	 $v = 'changed';
60  }
61  var_dump($b);
62  unset($a, $b);
63
64  echo "\n" . '$a->b[0]' . "\n";
65  $b = $a->b[0] = array('original');
66  foreach($a->b[0] as $k=>&$v) {
67  	 $v = 'changed';
68  }
69  var_dump($b);
70  unset($a, $b);
71
72  echo "\n" . '$a->b[0][0]' . "\n";
73  $b = $a->b[0][0] = array('original');
74  foreach($a->b[0][0] as $k=>&$v) {
75  	 $v = 'changed';
76  }
77  var_dump($b);
78  unset($a, $b);
79
80  echo "\n" . '$a->b[0]->c' . "\n";
81  $b = $a->b[0]->c = array('original');
82  foreach($a->b[0]->c as $k=>&$v) {
83  	 $v = 'changed';
84  }
85  var_dump($b);
86  unset($a, $b);
87
88  class C {
89  	public static $a;
90  }
91
92  echo "\n" . 'C::$a' . "\n";
93  C::$a = array('original');
94  $b = C::$a;
95  foreach(C::$a as $k=>&$v) {
96  	 $v = 'changed';
97  }
98  var_dump($b);
99  unset($a, $b);
100
101  echo "\n" . 'C::$a[0]' . "\n";
102  C::$a[0] = array('original');
103  $b = C::$a[0];
104  foreach(C::$a[0] as $k=>&$v) {
105  	 $v = 'changed';
106  }
107  var_dump($b);
108  unset(C::$a[0], $b);
109
110  echo "\n" . 'C::$a[0]->b' . "\n";
111  C::$a[0]->b = array('original');
112  $b = C::$a[0]->b;
113  foreach(C::$a[0]->b as $k=>&$v) {
114  	 $v = 'changed';
115  }
116  var_dump($b);
117  unset(C::$a[0]->b, $b);
118?>
119--EXPECTF--
120$a
121array(1) {
122  [0]=>
123  string(8) "original"
124}
125
126${'a'}
127array(1) {
128  [0]=>
129  string(8) "original"
130}
131
132$$a
133array(1) {
134  [0]=>
135  string(8) "original"
136}
137
138$a[0]
139array(1) {
140  [0]=>
141  string(8) "original"
142}
143
144$a[0][0]
145array(1) {
146  [0]=>
147  string(8) "original"
148}
149
150$a->b
151
152Warning: Creating default object from empty value in %s on line %d
153array(1) {
154  [0]=>
155  string(8) "original"
156}
157
158$a->b->c
159
160Warning: Creating default object from empty value in %s on line %d
161
162Warning: Creating default object from empty value in %s on line %d
163array(1) {
164  [0]=>
165  string(8) "original"
166}
167
168$a->b[0]
169
170Warning: Creating default object from empty value in %s on line %d
171array(1) {
172  [0]=>
173  string(8) "original"
174}
175
176$a->b[0][0]
177
178Warning: Creating default object from empty value in %s on line %d
179array(1) {
180  [0]=>
181  string(8) "original"
182}
183
184$a->b[0]->c
185
186Warning: Creating default object from empty value in %s on line %d
187
188Warning: Creating default object from empty value in %s on line %d
189array(1) {
190  [0]=>
191  string(8) "original"
192}
193
194C::$a
195array(1) {
196  [0]=>
197  string(8) "original"
198}
199
200C::$a[0]
201array(1) {
202  [0]=>
203  string(8) "original"
204}
205
206C::$a[0]->b
207
208Warning: Creating default object from empty value in %s on line %d
209array(1) {
210  [0]=>
211  string(8) "original"
212}
213