0

Trying to figure out why php anonymous functions only work when they are given parameters in the function header.

For example

$f = function(){
    echo "hello world";
};
$f;
$f();

won't work. But

$f = function($argument){
    echo $argument;
}
$f('hello world');

works just fine.

Why does it need arguments and is there any work around for this?

EDIT

This must be a version issue. I'm on 5.3.18 and my first example definitely doesn't work. For those not believing, it throws:

Parse error: syntax error, unexpected T_FUNCTION in index.php(192) : 
  eval()'d code on line 1

EDIT

After looking at DaveRandom's answer I'm back to having no idea what's happening. That is if they are correct that it works in 5.3.10 ...

3 Answers 3

5

This is perfectly valid syntax and outputs hello world:

$f = function(){
    echo "hello world";
};
$f();

The line $f; does nothing, and would be equivalent to declaring any other variable and then writing that new variable name and a semicolon.

Anonymous functions do not require parameters, see the manual for more details about them.

You are getting those syntax errors because you are running a PHP version < 5.3.

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

5 Comments

@DanielNill - Are you sure you're running PHP > 5.3? See my edit - Your errors are conducive of a mismatch.
It would seem so. php -version gives PHP 5.3.8 with Suhosin-Patch (cli) (built: Nov 15 2011 15:33:15) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies. But it must be a configuration issue or that the php version on my command line does not match the version on my xampp install. Closures were introduced in 5.3 so I would think that if they work with an argument in a given version they would work without arguments in that same version...
That might not be the case: php -version from the command line can invoke a different PHP than what's being executed on the server. Do echo phpversion(); in a PHP script and load that in your browser to see.
Yeah looks like it was a version issue. upgrading php seems to have fixed it. Wonder why passing an argument to the function worked...
@DanielNill I can find no record of this as a bug or feature change, since 5.3.8 or otherwise. May be a bug with suhosin? Maybe you had some other syntax error in your test case? It seems like you have now fixed this so it is now academic, but out of interest what error were you getting? E_PARSE or some kind of run-time error?
4

This doesn't invoke the closure:

$f;

But this one does:

$f();

Function calls require parens to be recognized by the parser. If you just mention the variable $f; then that's an empty expression. The closure object contained in $f gets assigned to a temporary zval (variable placeholder), then thrown away.

Comments

3

The first code works fine if you remove the meaningless $f; line.

Edit Actually, it still works even if you leave that line in. And in 5.3.10 as well.

6 Comments

It's only working in your example because of the $f();, if you remove that, it doesn't print anything.
@nickb Well yes, it you never call it it won't do anything... but surely that's a given?
I suppose I don't understand what you mean by "it works both ways" then. Which ways are you referring to?
@nickb whether you have the the useless statement $f in there or not
@nickb I thought that $f; on a line on it's own would throw an error, but apparently not. I just meant that you can leave the line in and it still works (it's more obvious if you check the codepads). I'll edit to make it clearer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.