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 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码