scastiel
scastiel

Reputation: 95

Closures with PHP

I have a weird problem trying to use closures in PHP. When assigning a closure to a variable, I get a null value. But when displaying the closure with var_dump(), everything is alright.

Here is a source code that summarizes the problem:

$f = function() {};
var_dump($f); // 'null'

var_dump(function() {}); // 'object(Closure)[1]'

I'm using PHP 5.3.1.

Edit: I forgot to mention, I have this problem only when I'm using PHP via Apache. I don't have issues when using PHP CLI.

Upvotes: 5

Views: 315

Answers (2)

scastiel
scastiel

Reputation: 95

A colleague found the answer to the problem: the responsible is eAccelerator! Apparently it's not compatible with PHP 5.3 closures... (source)

Disabling it solved the problem.

Thanks for your help!

Upvotes: 2

Narf
Narf

Reputation: 14752

It's either a very rare (and already fixed) bug or you're not showing the exact same usage that gives you a NULL. My guess is that you're doing this with the first var_dump():

var_dump($f());

Note the parenthesis, they cause the function to be run and therefore you get its return value.

Upvotes: 0

Related Questions