5

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.

10
  • 2
    but according to 3v4l.. it works.. weird. 3v4l.org/SFd06 Commented Nov 18, 2013 at 14:40
  • 1
    Weird. I'm getting this output (which is the correct one, AFAIK). Commented Nov 18, 2013 at 14:40
  • 2
    Try running that code exactly, to be sure your summary isn't different from your actual code. Commented Nov 18, 2013 at 14:41
  • John, this source code actually gives me this output. Commented Nov 18, 2013 at 14:46
  • 1
    @Jack It must be a bug. Because I just ran the code on 5.5.1 and it works as expected. So it's... FIXED. - hence it was a bug, logic says so. Unfortunately, I'm too busy to do bug research at the moment. And this bug would never hit me as I see absolutely no reason in writing an empty closure. I usually write functions that do stuff. Commented Nov 18, 2013 at 15:04

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Consider updating. They claim to support 5.4 on their site. PS: Is the first time you use a closure...? They're probably the most useful PHP feature added this decade and they've been around for ONLY 4 years.
If it depended on me I'll always use PHP 5.4 or 5.5, but I'm using in professional environment...
0

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.

1 Comment

Nope, I copy-pasted the code. When I write $f() I have an error. (It's logic since $f is evaluated to null.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.