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

时间:2021-08-07 22:04:10

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?

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

3 个解决方案

#1


44  

No, they are not passed by reference - the use follows a similar notation like the function's parameters. You can validate that on your own with the help of the debug_zval_dump function (Demo):

不,它们不是通过引用传递的——使用类似于函数参数的符号。通过debug_zval_dump函数(Demo),您可以自己验证它:

<?php
header('Content-Type: text/plain;');

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. As written you achieve that by defining the use as pass-by-reference:

一个完全作用域的引用将具有1的refcount。正如你所写的那样,你通过将使用定义为通过引用来实现这一点:

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

It's also possible to create recursion this way:

也可以这样创建递归:

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

#2


14  

Closures are, almost by definition, closed by value, not by reference. You may "use by reference" by adding an & in the argument list:

闭包几乎是由值而不是引用来关闭的。您可以通过在参数列表中添加一个&来“引用”:

function() use (&$var)

This can be seen in example 3 in the anonymous functions manual page.

在匿名函数手册页面的示例3中可以看到这一点。

#3


1  

No, they are not passed by reference.

不,它们不是通过引用传递的。

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

$my_var = 0;
foo($my_var);
echo $my_var; // displays 0

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

$my_var = 0;
bar($my_var);
echo $my_var; // displays 1

#1


44  

No, they are not passed by reference - the use follows a similar notation like the function's parameters. You can validate that on your own with the help of the debug_zval_dump function (Demo):

不,它们不是通过引用传递的——使用类似于函数参数的符号。通过debug_zval_dump函数(Demo),您可以自己验证它:

<?php
header('Content-Type: text/plain;');

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. As written you achieve that by defining the use as pass-by-reference:

一个完全作用域的引用将具有1的refcount。正如你所写的那样,你通过将使用定义为通过引用来实现这一点:

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

It's also possible to create recursion this way:

也可以这样创建递归:

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

#2


14  

Closures are, almost by definition, closed by value, not by reference. You may "use by reference" by adding an & in the argument list:

闭包几乎是由值而不是引用来关闭的。您可以通过在参数列表中添加一个&来“引用”:

function() use (&$var)

This can be seen in example 3 in the anonymous functions manual page.

在匿名函数手册页面的示例3中可以看到这一点。

#3


1  

No, they are not passed by reference.

不,它们不是通过引用传递的。

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

$my_var = 0;
foo($my_var);
echo $my_var; // displays 0

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

$my_var = 0;
bar($my_var);
echo $my_var; // displays 1