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 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏