xref: /PHP-5.5/tests/lang/foreachLoop.016.phpt (revision b2d38435)
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
122$a
123array(1) {
124  [0]=>
125  string(8) "original"
126}
127
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[0]
141array(1) {
142  [0]=>
143  string(8) "original"
144}
145
146$a[0][0]
147array(1) {
148  [0]=>
149  string(8) "original"
150}
151
152$a->b
153
154Warning: Creating default object from empty value in %s on line %d
155array(1) {
156  [0]=>
157  string(8) "original"
158}
159
160$a->b->c
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]
169array(1) {
170  [0]=>
171  string(8) "original"
172}
173
174$a->b[0][0]
175array(1) {
176  [0]=>
177  string(8) "original"
178}
179
180$a->b[0]->c
181
182Warning: Creating default object from empty value in %s on line %d
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
201
202Warning: Creating default object from empty value in %s on line %d
203array(1) {
204  [0]=>
205  string(8) "original"
206}
207