Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

New In Initializers

New in initializers, or the new initializer feature, makes it 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 Elephpant {
        function __construct(private int $i) {}
    }

    function headers($elephpant = new Elephpant(3)) : Elephpant {
        return $elephpant;
    }

?>

Documentation

See Also