Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP static anonymous call doesn't work

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

like image 571
Artem Zwinger Avatar asked May 26 '26 03:05

Artem Zwinger


1 Answers

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.

like image 129
apokryfos Avatar answered May 28 '26 17:05

apokryfos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!