如何在PHP中调用当前的匿名函数?

时间:2021-04-04 04:40:19

I have an anonymous function which is supposed to call itself. However, I have no variable or function name at hand, so I was hoping to find a function that could do return "this" in context of functions. Is there such a thing?

我有一个匿名函数,它应该调用自己。但是,我手头没有变量或函数名,所以我希望找到一个函数,可以在函数的上下文中返回“this”。有这样的事吗?

Here's an example:

这里有一个例子:

$f = function() use($bar, $foo) {
  // call this function again.
};

Calling like this:

要求是这样的:

call_user_func(__FUNCTION__);

Leads to this:

导致:

Warning: call_user_func() expects parameter 1 to be a valid callback, function '{closure}' not found or invalid function name

警告:call_user_func()期望参数1是一个有效的回调,函数“{闭包}”未找到或函数名无效

If I try to put $f in the use-list, then it says the variable is not defined (because it is not yet).

如果我尝试在use-list中放入$f,那么它表示变量没有定义(因为它还没有定义)。

5 个解决方案

#1


51  

__FUNCTION__ cannot be used in anonymous functions

__FUNCTION__不能在匿名函数中使用

Pass the variable holding the anonymous function as a reference in the 'use' clause....

通过变量持有的匿名函数作为参考使用的条款....

$f = function() use($bar, $foo, &$f) {
   $f();
};

Tip of the hat to this answer.

对这个答案顶礼膜拜。

#2


7  

Okay, I found out the way to do this:

好的,我找到了这样做的方法:

$f = function() use(&$f) {
  $f();
};

$f();

The key thing is to pass $f as a reference. Thus PHP does not try to pass a value but a reference to a memory slot.

关键是传递$f作为引用。因此,PHP不尝试传递值,而是传递对内存槽的引用。

#3


1  

I have an anonymous function which is supposed to call itself.

我有一个匿名函数,它应该调用自己。

I prefer to use call_user_func_array(__FUNCTION__, $params); when calling a recursive function.

我更喜欢使用call_user_func_array(__FUNCTION__, $params);调用递归函数时。

As your example doesn't have any arguments then i guess call_user_func(__FUNCTION__); would be better suited.

因为你的例子没有任何参数,所以我猜call_user_func(__FUNCTION__);会更适合。

You would expect and hope the following code would work but that would be too easy.

您可能期望并希望下面的代码能够工作,但这太容易了。

$bar = 10;
$foo = 0;

$f = function() use (&$bar,$foo) {

    if($bar){ // condition needed to prevent infinite loop
        echo $bar-- . PHP_EOL;
        call_user_func(__FUNCTION__); // wont work
    }
};
$f();

The __FUNCTION__ "Magic constant" is unavailable to closures so the code needs to be adapted to allow the passing of the function variable. we can make the function available by passing it as a regular argument or via the use statement.

__FUNCTION__“Magic常量”对于闭包是不可用的,因此需要修改代码以允许函数变量的传递。我们可以通过将函数作为常规参数传递或通过use语句传递给它,从而使其可用。

Function passed as argument

函数作为参数传递

$bar = 10;
$foo = 0;

$f = function( $__FUNCTION__ = null ) use (&$bar, $foo) {

    if($__FUNCTION__ && $bar){
        echo $bar-- . PHP_EOL;
        call_user_func( $__FUNCTION__, $__FUNCTION__);
    }
};
$f ( $f );

Function passed via use statement

通过使用语句传递的函数

$bar = 10;
$foo = 0;

$__FUNCTION__ = function() use (&$bar, $foo, &$__FUNCTION__) {

    if($bar){
        echo $bar-- . PHP_EOL;
        call_user_func( $__FUNCTION__ );
    }
};
$__FUNCTION__();

Working example, click edit-> ideone it! to re-run code.

工作示例,点击编辑-> ideone it!重新运行代码。

#4


-1  

http://www.php.net/manual/en/language.constants.predefined.php

http://www.php.net/manual/en/language.constants.predefined.php

Edit: Posted before code was given. Of course it doesn't work on anonymous functions.

编辑:在给出代码之前发布。当然,它对匿名函数不起作用。

call_user_func(__FUNCTION__, $param1, $param2);
call_user_func_array(__FUNCTION__, $params);

#5


-4  

function i_dont_know() {
    call_user_func(__FUNCTION__,$params);
 //or
    $funcname = __FUNCTION__;
    $funcname($params);


}

#1


51  

__FUNCTION__ cannot be used in anonymous functions

__FUNCTION__不能在匿名函数中使用

Pass the variable holding the anonymous function as a reference in the 'use' clause....

通过变量持有的匿名函数作为参考使用的条款....

$f = function() use($bar, $foo, &$f) {
   $f();
};

Tip of the hat to this answer.

对这个答案顶礼膜拜。

#2


7  

Okay, I found out the way to do this:

好的,我找到了这样做的方法:

$f = function() use(&$f) {
  $f();
};

$f();

The key thing is to pass $f as a reference. Thus PHP does not try to pass a value but a reference to a memory slot.

关键是传递$f作为引用。因此,PHP不尝试传递值,而是传递对内存槽的引用。

#3


1  

I have an anonymous function which is supposed to call itself.

我有一个匿名函数,它应该调用自己。

I prefer to use call_user_func_array(__FUNCTION__, $params); when calling a recursive function.

我更喜欢使用call_user_func_array(__FUNCTION__, $params);调用递归函数时。

As your example doesn't have any arguments then i guess call_user_func(__FUNCTION__); would be better suited.

因为你的例子没有任何参数,所以我猜call_user_func(__FUNCTION__);会更适合。

You would expect and hope the following code would work but that would be too easy.

您可能期望并希望下面的代码能够工作,但这太容易了。

$bar = 10;
$foo = 0;

$f = function() use (&$bar,$foo) {

    if($bar){ // condition needed to prevent infinite loop
        echo $bar-- . PHP_EOL;
        call_user_func(__FUNCTION__); // wont work
    }
};
$f();

The __FUNCTION__ "Magic constant" is unavailable to closures so the code needs to be adapted to allow the passing of the function variable. we can make the function available by passing it as a regular argument or via the use statement.

__FUNCTION__“Magic常量”对于闭包是不可用的,因此需要修改代码以允许函数变量的传递。我们可以通过将函数作为常规参数传递或通过use语句传递给它,从而使其可用。

Function passed as argument

函数作为参数传递

$bar = 10;
$foo = 0;

$f = function( $__FUNCTION__ = null ) use (&$bar, $foo) {

    if($__FUNCTION__ && $bar){
        echo $bar-- . PHP_EOL;
        call_user_func( $__FUNCTION__, $__FUNCTION__);
    }
};
$f ( $f );

Function passed via use statement

通过使用语句传递的函数

$bar = 10;
$foo = 0;

$__FUNCTION__ = function() use (&$bar, $foo, &$__FUNCTION__) {

    if($bar){
        echo $bar-- . PHP_EOL;
        call_user_func( $__FUNCTION__ );
    }
};
$__FUNCTION__();

Working example, click edit-> ideone it! to re-run code.

工作示例,点击编辑-> ideone it!重新运行代码。

#4


-1  

http://www.php.net/manual/en/language.constants.predefined.php

http://www.php.net/manual/en/language.constants.predefined.php

Edit: Posted before code was given. Of course it doesn't work on anonymous functions.

编辑:在给出代码之前发布。当然,它对匿名函数不起作用。

call_user_func(__FUNCTION__, $param1, $param2);
call_user_func_array(__FUNCTION__, $params);

#5


-4  

function i_dont_know() {
    call_user_func(__FUNCTION__,$params);
 //or
    $funcname = __FUNCTION__;
    $funcname($params);


}