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