douzhuiqing1151 2014-08-06 13:04
浏览 104
已采纳

匿名函数的变量赋值返回null

I have the following lines of code in my application:

$d = function(){
    return 5;
};
var_dump($d, gettype($d));
exit;

When I execute this code in my application it outputs:

NULL string(4) "NULL"

This surprises me, how can $d by NULL after it has been assigned an anonymous function?

When I run the exact same code in a separate PHP file on the same webserver under the same vhost / config it outputs:

object(Closure)#1 (0) {} string(6) "object"

So it seems to be something with my PHP application which is built with Zend Framework. But I can't understand what my application could have done to mess up this core behavior. I am experiencing this problem with PHP 5.3.2 on CentOS 5.8. Any directions on whats going on here are welcome since I'm all out of ideas on this one.

Edit: Also when trying to running $d() in the non-working case PHP says:

Fatal error: Function name must be a string in /Bootstrap.php on line 118

Strange wording of the error since the language now accepts anonymous functions, but this code gives me te same error so it seems to be the correct message:

$a = null;
$a();
  • 写回答

3条回答 默认 最新

  • doufu1504 2014-08-06 13:52
    关注

    Okay, so the key part of information that I forgot to provide (or essentially missed while debugging) is that in my application the non-working closure is the second closure in the execution of the script. In my demo code the closure is the first closure in the script and therefor working. There seems to be a problem with my version / build of PHP and multiple closures. For the record these are this is the version information of PHP on which I am experiencing this problem:

    PHP Version => 5.3.2
    System => Linux ip-172-31-15-243 2.6.18-308.16.1.el5.centos.plusxen #1 SMP Tue Oct 2     23:25:27 EDT 2012 x86_64
    Build Date => Jun  6 2013 09:58:54
    

    The following code provides a good test:

    $closures[] = array();
    for ($x = 0; $x < 5; $x++) {
        $closures[$x] = function() use($x) {
            return $x * 2;
        };
    }
    var_dump($closures);
    

    On the broken PHP 5.3.2 this outputs:

    array(5) {
      [0]=>
      NULL
      [1]=>
      NULL
      [2]=>
      NULL
      [3]=>
      NULL
      [4]=>
      NULL
    }
    

    On PHP 5.4.24 on my MacBook this outputs:

    array(5) {
      [0]=>
      object(Closure)#1 (1) {
        ["static"]=>
        array(1) {
          ["x"]=>
          int(0)
        }
      }
      [1]=>
      object(Closure)#2 (1) {
        ["static"]=>
        array(1) {
          ["x"]=>
          int(1)
        }
      }
      [2]=>
      object(Closure)#3 (1) {
        ["static"]=>
        array(1) {
          ["x"]=>
          int(2)
        }
      }
      [3]=>
      object(Closure)#4 (1) {
        ["static"]=>
        array(1) {
          ["x"]=>
          int(3)
        }
      }
      [4]=>
      object(Closure)#5 (1) {
        ["static"]=>
        array(1) {
          ["x"]=>
          int(4)
        }
      }
    }
    

    To make matters even stranger, when I run the script on the command line with the php command the output is correct. When I run the the script via the php-cgi command (which is used by the webserver) the output is wrong. So for now this problem seems to have to do with php-cgi.

    Edit:

    The solution: eAccelerator is the problem here. I'm using eAccelerator v0.9.6, when this extension is disabled everything works as expected. When this extension is enabled the callable are NULL. See eAccelerator issue at Github repo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算