Reputation: 146
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();
Upvotes: 0
Views: 664
Reputation: 146
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.
Upvotes: 2
Reputation: 1803
May be you have an old php
. Upgrade it please.
I have next result:
class Closure#1 (0) {
}
string(6) "object"
Upvotes: 0
Reputation: 1662
Try this
<?php
function abc (){
return 5;
};
$d =abc();
var_dump($d);
exit;
?>
Upvotes: 0