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 or class constant, and available at calling time.

<?php

    class B {
        function __construct(private int $i) {}
    }

    function headers($a = new B(3)) : B {
        return $a;
    }

?>

Documentation

See also PHP RFC: New in initializers.

Related : new, Static Variables, Constants, Static Constant, Parameter

Added in PHP 8.1+