1--TEST--
2array_walk() closure tests
3--FILE--
4<?php
5
6$ar = ["one" => 1, "two"=>2, "three" => 3];
7var_dump(array_walk($ar, function(){ var_dump(func_get_args());}));
8
9echo "\nclosure with array\n";
10$ar = ["one" => 1, "two"=>2, "three" => 3];
11$user_data = ["sum" => 42];
12$func = function($value, $key, &$udata) {
13    var_dump($udata);
14    $udata["sum"] += $value;
15};
16
17var_dump(array_walk($ar, $func, $user_data));
18echo "End result:";
19var_dump($user_data["sum"]);
20
21echo "\nclosure with use\n";
22$ar = ["one" => 1, "two"=>2, "three" => 3];
23$user_data = ["sum" => 42];
24$func = function($value, $key) use (&$user_data) {
25    var_dump($user_data);
26    $user_data["sum"] += $value;
27};
28
29var_dump(array_walk($ar, $func, $user_data));
30echo "End result:";
31var_dump($user_data["sum"]);
32
33
34echo "\nclosure with object\n";
35$ar = ["one" => 1, "two"=>2, "three" => 3];
36$user_data = (object)["sum" => 42];
37$func = function($value, $key, &$udata) {
38    var_dump($udata);
39    $udata->sum += $value;
40};
41
42var_dump(array_walk($ar, $func, $user_data));
43echo "End result:";
44var_dump($user_data->sum);
45
46
47
48echo "\nfunction with object\n";
49function sum_it_up_object($value, $key, $udata)
50{
51    var_dump($udata);
52    $udata->sum += $value;
53}
54
55$ar = ["one" => 1, "two"=>2, "three" => 3];
56$user_data = (object)["sum" => 42];
57
58var_dump(array_walk($ar, "sum_it_up_object", $user_data));
59echo "End result:";
60var_dump($user_data->sum);
61
62
63echo "\nfunction with array\n";
64function sum_it_up_array($value, $key, $udata)
65{
66    var_dump($udata);
67    $udata['sum'] += $value;
68}
69
70$ar = ["one" => 1, "two"=>2, "three" => 3];
71$user_data = ["sum" => 42];
72
73var_dump(array_walk($ar, "sum_it_up_array", $user_data));
74echo "End result:";
75var_dump($user_data['sum']);
76
77echo "\nclosure and exception\n";
78$ar = ["one" => 1, "two"=>2, "three" => 3];
79try {
80    var_dump(array_walk($ar, function($v, $k) { if ($v == 2) throw new Exception; } ));
81} catch (Exception $e) {
82    var_dump($e->getTrace());
83}
84
85
86echo "Done\n";
87?>
88--EXPECTF--
89array(2) {
90  [0]=>
91  int(1)
92  [1]=>
93  string(3) "one"
94}
95array(2) {
96  [0]=>
97  int(2)
98  [1]=>
99  string(3) "two"
100}
101array(2) {
102  [0]=>
103  int(3)
104  [1]=>
105  string(5) "three"
106}
107bool(true)
108
109closure with array
110
111Warning: {closure}(): Argument #3 ($udata) must be passed by reference, value given in %s on line %d
112array(1) {
113  ["sum"]=>
114  int(42)
115}
116
117Warning: {closure}(): Argument #3 ($udata) must be passed by reference, value given in %s on line %d
118array(1) {
119  ["sum"]=>
120  int(42)
121}
122
123Warning: {closure}(): Argument #3 ($udata) must be passed by reference, value given in %s on line %d
124array(1) {
125  ["sum"]=>
126  int(42)
127}
128bool(true)
129End result:int(42)
130
131closure with use
132array(1) {
133  ["sum"]=>
134  int(42)
135}
136array(1) {
137  ["sum"]=>
138  int(43)
139}
140array(1) {
141  ["sum"]=>
142  int(45)
143}
144bool(true)
145End result:int(48)
146
147closure with object
148
149Warning: {closure}(): Argument #3 ($udata) must be passed by reference, value given in %s on line %d
150object(stdClass)#1 (1) {
151  ["sum"]=>
152  int(42)
153}
154
155Warning: {closure}(): Argument #3 ($udata) must be passed by reference, value given in %s on line %d
156object(stdClass)#1 (1) {
157  ["sum"]=>
158  int(43)
159}
160
161Warning: {closure}(): Argument #3 ($udata) must be passed by reference, value given in %s on line %d
162object(stdClass)#1 (1) {
163  ["sum"]=>
164  int(45)
165}
166bool(true)
167End result:int(48)
168
169function with object
170object(stdClass)#2 (1) {
171  ["sum"]=>
172  int(42)
173}
174object(stdClass)#2 (1) {
175  ["sum"]=>
176  int(43)
177}
178object(stdClass)#2 (1) {
179  ["sum"]=>
180  int(45)
181}
182bool(true)
183End result:int(48)
184
185function with array
186array(1) {
187  ["sum"]=>
188  int(42)
189}
190array(1) {
191  ["sum"]=>
192  int(42)
193}
194array(1) {
195  ["sum"]=>
196  int(42)
197}
198bool(true)
199End result:int(42)
200
201closure and exception
202array(2) {
203  [0]=>
204  array(2) {
205    ["function"]=>
206    string(9) "{closure}"
207    ["args"]=>
208    array(2) {
209      [0]=>
210      int(2)
211      [1]=>
212      string(3) "two"
213    }
214  }
215  [1]=>
216  array(4) {
217    ["file"]=>
218    string(%d) "%s"
219    ["line"]=>
220    int(%d)
221    ["function"]=>
222    string(10) "array_walk"
223    ["args"]=>
224    array(2) {
225      [0]=>
226      array(3) {
227        ["one"]=>
228        int(1)
229        ["two"]=>
230        int(2)
231        ["three"]=>
232        int(3)
233      }
234      [1]=>
235      object(Closure)#2 (1) {
236        ["parameter"]=>
237        array(2) {
238          ["$v"]=>
239          string(10) "<required>"
240          ["$k"]=>
241          string(10) "<required>"
242        }
243      }
244    }
245  }
246}
247Done
248