<tfoot id='AXFsC'></tfoot>
    <bdo id='AXFsC'></bdo><ul id='AXFsC'></ul>
<i id='AXFsC'><tr id='AXFsC'><dt id='AXFsC'><q id='AXFsC'><span id='AXFsC'><b id='AXFsC'><form id='AXFsC'><ins id='AXFsC'></ins><ul id='AXFsC'></ul><sub id='AXFsC'></sub></form><legend id='AXFsC'></legend><bdo id='AXFsC'><pre id='AXFsC'><center id='AXFsC'></center></pre></bdo></b><th id='AXFsC'></th></span></q></dt></tr></i><div id='AXFsC'><tfoot id='AXFsC'></tfoot><dl id='AXFsC'><fieldset id='AXFsC'></fieldset></dl></div>

  • <small id='AXFsC'></small><noframes id='AXFsC'>

      <legend id='AXFsC'><style id='AXFsC'><dir id='AXFsC'><q id='AXFsC'></q></dir></style></legend>

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

        Does the use keyword in PHP closures pass by reference?(PHP 闭包中的 use 关键字是否通过引用传递?)

        <small id='5ZQIp'></small><noframes id='5ZQIp'>

          <tbody id='5ZQIp'></tbody>

          <i id='5ZQIp'><tr id='5ZQIp'><dt id='5ZQIp'><q id='5ZQIp'><span id='5ZQIp'><b id='5ZQIp'><form id='5ZQIp'><ins id='5ZQIp'></ins><ul id='5ZQIp'></ul><sub id='5ZQIp'></sub></form><legend id='5ZQIp'></legend><bdo id='5ZQIp'><pre id='5ZQIp'><center id='5ZQIp'></center></pre></bdo></b><th id='5ZQIp'></th></span></q></dt></tr></i><div id='5ZQIp'><tfoot id='5ZQIp'></tfoot><dl id='5ZQIp'><fieldset id='5ZQIp'></fieldset></dl></div>
        1. <legend id='5ZQIp'><style id='5ZQIp'><dir id='5ZQIp'><q id='5ZQIp'></q></dir></style></legend>
            <bdo id='5ZQIp'></bdo><ul id='5ZQIp'></ul>

                <tfoot id='5ZQIp'></tfoot>

                • 本文介绍了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) 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.

                  这篇关于PHP 闭包中的 use 关键字是否通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                • <small id='GGnpI'></small><noframes id='GGnpI'>

                  <legend id='GGnpI'><style id='GGnpI'><dir id='GGnpI'><q id='GGnpI'></q></dir></style></legend>
                  <tfoot id='GGnpI'></tfoot>
                    <bdo id='GGnpI'></bdo><ul id='GGnpI'></ul>
                      <i id='GGnpI'><tr id='GGnpI'><dt id='GGnpI'><q id='GGnpI'><span id='GGnpI'><b id='GGnpI'><form id='GGnpI'><ins id='GGnpI'></ins><ul id='GGnpI'></ul><sub id='GGnpI'></sub></form><legend id='GGnpI'></legend><bdo id='GGnpI'><pre id='GGnpI'><center id='GGnpI'></center></pre></bdo></b><th id='GGnpI'></th></span></q></dt></tr></i><div id='GGnpI'><tfoot id='GGnpI'></tfoot><dl id='GGnpI'><fieldset id='GGnpI'></fieldset></dl></div>

                            <tbody id='GGnpI'></tbody>