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 '->')

2 Answers2

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.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
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

Community
  • 1
  • 1
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50