xref: /PHP-7.3/tests/lang/foreachLoop.016.phpt (revision b746e698)
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
161array(1) {
162  [0]=>
163  string(8) "original"
164}
165
166$a->b[0]
167array(1) {
168  [0]=>
169  string(8) "original"
170}
171
172$a->b[0][0]
173array(1) {
174  [0]=>
175  string(8) "original"
176}
177
178$a->b[0]->c
179
180Warning: Creating default object from empty value in %s on line %d
181array(1) {
182  [0]=>
183  string(8) "original"
184}
185
186C::$a
187array(1) {
188  [0]=>
189  string(8) "original"
190}
191
192C::$a[0]
193array(1) {
194  [0]=>
195  string(8) "original"
196}
197
198C::$a[0]->b
199
200Warning: Creating default object from empty value in %s on line %d
201array(1) {
202  [0]=>
203  string(8) "original"
204}
205