That is variables that are visible to anonymous functions iside a main function. I ask because array_walk_recursive allow for only one additional parameter, it would be nice to be able to reach some vars from anonymous functions without relying on constant compact and extract
Asked
Active
Viewed 94 times
3 Answers
3
Closure can:
function() use ($var) { /*code*/}
Use use(vars)
to close over these variables in your function.
Can't post a link to the official use docu because php.net lacks there

dynamic
- 46,985
- 55
- 154
- 231
-
yes that links that totally lacks on a good explanation of it – dynamic Jun 21 '11 at 16:43
-
there is the link of the rfc on wiki. that is a good explanation. of course i will not search for it. I dunno neither if this is a good answer for the OP – dynamic Jun 21 '11 at 16:44
-
1When you use `use` it isnt a Lambda, but a Closure and technically you are not accessing external scope variables, but you are closing over these variables when the Closure is defined. See [What is the difference between a closure and a lambda](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda). From a pragmatic point of view, it might yield the desired results though. – Gordon Jun 21 '11 at 16:46
-
http://www.ibm.com/developerworks/opensource/library/os-php-5.3new2/index.html I found this to be helpful – grep Jun 21 '11 at 17:52
-
@Headspin it should be noted though that the IBM article does not reflect the current implementation of Closures. [You cannot use $this inside a Closure in PHP 5.3](https://wiki.php.net/rfc/closures/removal-of-this) so you should not take everything in that article for fact. It's still an acceptable overview though. – Gordon Jun 21 '11 at 18:41
-
ibm is becoming a pretty bad content farm – dynamic Jun 21 '11 at 19:02
1
If you want to pass multiple arguments to array_walk_recursive
, pass them as an array:
array_walk_recursive($data, 'callbackFn', array('arg1', new Foo, $bar));
Inside the callback, you can then access each additional argument by array index.
Example (demo)
$data = range(1, 10);
array_walk_recursive(
$data,
function(&$value, $key, $args)
{
$value = $value * $args[0] * $args[1] * $args[2];
},
array(3, 6, 7)
);
print_r($data);

Gordon
- 312,688
- 75
- 539
- 559
-
-
@user it creates an instance of Foo. It was just an example to show that you can add basically anything you want for additional arguments to that function. You are not limited to one argument. – Gordon Jun 21 '11 at 17:10
-1
Using an object as a function via __invoke
can be a useful alternative to closures:
<?php
class Walker
{
public function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
public function __invoke($val, $key)
{
if ($val == $this->a || $val == $this->b)
echo "$key\n";
}
}
$a = array('the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog');
array_walk($a, new Walker('quick', 'dog'));
Obviously it's more verbose, but when you are doing more than just a simple task, it can be a little easier to follow.

Matthew
- 47,584
- 11
- 86
- 98
-
`__invoke` only works in PHP 5.3+, in which case you should really use closures – user102008 Jun 09 '12 at 08:00
-
@user102008, for "read-only" closures ... yes, the object is unnecessary. And usually that is the case, but sometimes an object that implements `__invoke` is useful. I agree that a closure is the correct tool for this question; I was simply pointing out an alternative. – Matthew Jun 09 '12 at 14:04