New In Initializers¶
It is possible to use a new expression for default values of static variables, parameter and constants.
It is not possible to use a new operator in a property definition. All elements of the instantiation must be constants (literal or constants) and available at calling time.
<?php
class B {
function __construct(private int $i) {}
}
function headers($a = new B(3)) : B {
return $a;
}
?>
See also https://wiki.php.net/rfc/new_in_initializers
Related : new, Static Variables, Constants, Static Constant, Parameter
Added in PHP 8.1+