is that possible to call lambda function in nesting way
<?php
$func=function() use($something,$func /** as you know it will be undefined so what could be other way arround**/){
if($something){
$func();
}
}
is that possible to call lambda function in nesting way
<?php
$func=function() use($something,$func /** as you know it will be undefined so what could be other way arround**/){
if($something){
$func();
}
}
$func
is not defined yet when you pass it to $func
. $func
will only be defined right after the function definition, which is a little bit too late for this to work.
The easy work around is as follows:
$func = null;
$func = function() use (&$func) {
}
use $func
can't succeed, because $func
won't be defined until AFTER the lambda creation has completed and returned the lamba. PHP cannot time travel.
You also cannot do something like:
$func = '';
$func = function() use $something, $func ....;
While that gets around the $func is not defined
, it also "locks" the value of $func
into the lambda at the time the lambda's created:
php > $foo = 'bar';
php > $baz = function() use($foo) { echo $foo; };
php > $baz();
bar
php > $foo = 'qux';
php > $baz();
bar