__invoke() On New¶
<?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
?>
It is possible to create a new object by calling new on a previous object: PHP fetches the name of the class, then its constructor.
With the PHP 8.4 new syntax without parenthesis, it is also possible to chain the instantiation with a direct call. This calls the __invoke method.
This was not possible in previous versions.
See Also¶
invoke on new [Try me]
PHP Features¶
Last updated: 14 July 2026