-1

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();
  }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
dev.meghraj
  • 8,542
  • 5
  • 38
  • 76

2 Answers2

5

$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) {

}
Evert
  • 93,428
  • 18
  • 118
  • 189
0

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
Marc B
  • 356,200
  • 43
  • 426
  • 500