PHP 闭包中的 use 关键字是否通过引用传递?

2022-01-02 00:00:00 function functional-programming php

例如,如果我这样做:

功能栏(&$var){$foo = function() 使用 ($var){$var++;};$foo();}$my_var = 0;酒吧($my_var);

$my_var 会被修改吗?如果没有,我如何在不向 $foo 添加参数的情况下使其工作?

解决方案

不,它们不是通过引用传递 - use 遵循与函数参数类似的符号.

如所写,您通过将 use 定义为 pass-by-reference 来实现这一点:

 $foo = function() 使用 (&$var)

也可以通过这种方式创建递归:

$func = NULL;$func = function () 使用 (&$func) {$func();}


<块引用>

注意:以下旧答案摘录(2012 年 6 月)是为 PHP 编写的7.0.从 7.0(2015 年 12 月)开始,debug_zval_dump() 的语义发生了变化(不同的 zval 处理),它的 refcount(?) 输出现在有所不同,并没有太多说明更长(整数不再有引用计数).

通过不显示改变的 $my_var(从 0)通过输出进行验证仍然有效(行为).

您可以在 debug_zval_dump 函数的帮助下自行验证(演示):

功能栏(&$var){$foo = function() 使用 ($var){debug_zval_dump($var);$var++;};$foo();};$my_var = 0;酒吧($my_var);回声 $my_var;

输出:

long(0) refcount(3)0

一个完整到所有范围的工作引用的引用计数为 1.

For example, if I do this:

function bar(&$var)
{
    $foo = function() use ($var)
    {
        $var++;
    };
    $foo();
}

$my_var = 0;
bar($my_var);

Will $my_var be modified? If not, how do I get this to work without adding a parameter to $foo?

解决方案

No, they are not passed by reference - the use follows a similar notation like the function's parameters.

As written you achieve that by defining the use as pass-by-reference:

    $foo = function() use (&$var)

It's also possible to create recursion this way:

$func = NULL;
$func = function () use (&$func) {
    $func();
}


NOTE: The following old excerpt of the answer (Jun 2012) was written for PHP < 7.0. As since 7.0 (Dec 2015) the semantics of debug_zval_dump() changed (different zval handling) the refcount(?) output of it differs nowadays and are not that much saying any longer (integers don't have a refcount any longer).

Validation via the output by not displaying $my_var changed (from 0) still works though (behaviour).

You can validate that on your own with the help of the debug_zval_dump function (Demo):

function bar(&$var)
{
    $foo = function() use ($var)
    {
        debug_zval_dump($var);
        $var++;
    };
    $foo();
};
    
$my_var = 0;
bar($my_var);
echo $my_var;

Output:

long(0) refcount(3)
0

A full-through-all-scopes-working reference would have a refcount of 1.

相关文章