I am looking at the PHP example of Closures
on http://us1.php.net/manual/en/functions.anonymous.php
It provides the example code below and states:
You're misunderstanding the function signature. $quantity
and $product
are the regular arguments that will be passed into the function when it's called, they indeed do not exist in the parent scope and aren't meant to. use ($tax, &$total)
are the closed over variables from the parent scope.
$foo = 'foo'; // closed over variable
// vvvv
$func = function ($bar) use ($foo) {
// ^^^^
// regular function argument
return $foo . $bar;
};
echo $func('baz'); // "foobaz"
The two variable is question are what get passed into the callback by array_walk.
The first parameter will be passed as the value of each of the elements in the array, the second will be the key of the array.
The closed over variables are the ones referenced in the use
clause.
The closure arguments $quantity
and $product
do not exist per se in the function definition, they are just placeholders that array_walk will fill with real values during its execution procedure. The use
arguments are extra variables that you import into the array_walk call's scope that otherwise would not be available to the function.