问题描述
例如,如果我这样做:
功能栏(&$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 关键字是否通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)