Direct calls on new¶
Calling an object directly upon instantiation was not possible in PHP 8.3: it required parenthesis, like every other new
call.
In PHP 8.4, it is now possible to call a method or access a property directly at instantiation time. It is also possible to call its __invoke
method.
PHP code¶
<?php
class x {
function __construct($i = 0) { echo __METHOD__.PHP_EOL;}
function __invoke() { echo __METHOD__.PHP_EOL;}
}
$x = new x;
$y = new $x()();
// identical to
//$y = (new $x(0)) ()
var_dump($y);
// NULL
?>
Before¶
PHP Parse error: syntax error, unexpected token "(" in /codes/new_then_invoke.php on line 11
Parse error: syntax error, unexpected token "(" in /codes/new_then_invoke.php on line 11
After¶
x::__construct
x::__construct
x::__invoke
NULL
PHP version change¶
This behavior changed in 8.4