Does the use keyword in PHP closures pass by reference?(PHP 闭包中的 use 关键字是否通过引用传递?)
问题描述
例如,如果我这样做:
功能栏(&$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) therefcount(?)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_varchanged (from0) 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.
这篇关于PHP 闭包中的 use 关键字是否通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 闭包中的 use 关键字是否通过引用传递?
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
