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