3

PHP 5.6, Apache 2.4 | Windows 7, OpenServer

(static function () {
    return true;
})();

Why this throws syntax error?

syntax error, unexpected '('

But http://php.net/manual/en/functions.anonymous.php
PS: Also ->call doesn't work too... (unexpected '->')

0

2 Answers 2

2

Here's the problem. It's not the static part that's not working:

$f = static function () { return true; }; $f(); //Works in PHP 5.4+

It's the declaring and calling that doesn't work:

(static function () { return true; })(); //Works in PHP 7+

Problem is the documentation is saying that the first syntax is valid in PHP 5.4+ but uses examples that require PHP 7+ to work.

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

Comments

1

This doesn't work in PHP 5.x

You'll need PHP 7+ to run it.

Read more about it: https://stackoverflow.com/a/3605701/372172

1 Comment

Odd how the manual seems to imply that it should also work in 5.4+

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.