Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121442 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28023 invoked from network); 23 Oct 2023 14:19:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Oct 2023 14:19:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 59A3818050B for ; Mon, 23 Oct 2023 07:19:01 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,BODY_8BITS, KHOP_HELO_FCRDNS,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS16276 192.99.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mail.processus.org (ns563681.ip-192-99-44.net [192.99.44.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 23 Oct 2023 07:19:00 -0700 (PDT) Message-ID: <5be74be4-972f-4eae-ae46-6d92587ccc7f@processus.org> Authentication-Results: mail.processus.org; auth=pass smtp.mailfrom=pierre-php@processus.org Date: Mon, 23 Oct 2023 16:18:58 +0200 MIME-Version: 1.0 To: PHP internals Content-Language: fr-FR, en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spamd-Bar: / Subject: Constructor promoted property and default value From: pierre-php@processus.org (Pierre) Hello internals, I stumbled upon this behavior, if I write this: ```php 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: ```php 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