Constructor promoted property and default value

From: Date: Mon, 23 Oct 2023 14:18:58 +0000
Subject: Constructor promoted property and default value
Groups: php.internals 
Request: Send a blank email to internals+get-121442@lists.php.net to get a copy of this message
Hello internals, I stumbled upon this behavior, if I write this:
class Foo
{
    public ?string $prop = null;

    public function __construct(?string $prop = null)
    {
        $this->prop = $prop;
    }
}

class Bar extends Foo
{
    public function __construct(
        public ?string $bar = null,
    ) {}
}

// Echoes nothing, but it works as expected.
echo (new Bar())->prop;
It works as intended, but if I replace the Foo class using:
class Foo
{
    public function __construct(
        public ?string $prop = null,
    ) {}
}
It won't work anymore and I have the following error:
PHP Warning:  Uncaught Error: Typed property Foo::$prop must not be accessed before initialization in php shell code:9
If I understand it correctly:  - in the first case, default value is attached to the object property, so if I omit its constructor, I have the default,  - in the second case, default value is attached to the constructor parameter, and not to the object property, which means that in case the parent constructor is not called in the Bar class, $prop remains initialized. It doesn't sound like a bug, but I think that many people would actually expect otherwise: that the constructor promoted property keep their default even when constructor is not explicitly called. Is there any good reason behind this ? Wouldn't it be best to change this behavior ? Would it be a risk for backward compatibility (my guess is "not that much, probably not a all even") ? Regards, -- Pierre

« previous php.internals (#121442) next »