syntax error, unexpected token “(”

Description

Parenthesis cannot be placed anywhere, although they have a wide range of applications. In particular, they are used to call methods, by delimiting arguments; and set part of expressions apart.

A class cannot hold a closure, only method, which are functions with a name.

The syntax to call a method on an instantiation new x()->foo() and new x()() has been introduced in PHP 8.4.

Example

<?php

// methods cannot be closure: they need a name
class X {
    function () {}
}

// This is a PHP 8.4 syntax, cannot be used before.
new $x()();
// equivalent to (new $x())->__invoke();

?>

Solutions

  • Add the name to the method.