Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121444 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 32868 invoked from network); 23 Oct 2023 15:16:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Oct 2023 15:16:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 560AF1804B0 for ; Mon, 23 Oct 2023 08:16:48 -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=-1.3 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 23 Oct 2023 08:16:47 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 5489D5C031A for ; Mon, 23 Oct 2023 11:16:47 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Mon, 23 Oct 2023 11:16:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm3; t=1698074207; x=1698160607; bh=Z7wAMlu7dC HEZvYE2goTbbg7HhjP4kgXlfm+3BXghJs=; b=nqf+zBmxIgoLESHYkkEYWz35aw T5DUVx8Z+HkPlxkwIj3v/MHU3pReGZJhKTAai0iQFfi1mXeHmIm7WBmo44s9ZCvv TSIr6cC8WSAavs0/xldg68tiFuk+06w4QVYpvjNrhiPetKeegJwvNbj0OWOanPdg LOBqfSegPMGQMPrAdl2n4BHDONDKuNRwzTSxn3s54Mut0vTyisVQaW8DpqzWpRbW CjGZ9le/zglAwIocCCABs46oB59hTQ87+OA+qINSyOQG5iwqQCAAUp8wQmLoI7j4 5uT0nrAQMdPb96fDWLcVEagCVZaHKZ554oATCo0Nlf7zxuxpb7DGZ6pWo22w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1698074207; x= 1698160607; bh=Z7wAMlu7dCHEZvYE2goTbbg7HhjP4kgXlfm+3BXghJs=; b=R kso3IgDT2N4qmgADO5GTzgh6UoBX4Eh0x1UmWaDDdTXyQZvxqMQYXv4oHfDGfIVB Fm3fj+HddvRw+36Eg7Nl3bQFx5tY70tfXR0fLjWXsIqMSnWhyKdfXRBElcXEfwNN pOawJZu72xTTM6T8UOFE3oV3Y9g3aQW36h4j+gauLAg2NDC+2JYt7KTHDlBgHO2J TIhI9FgqsLVhyk/ckz4+VFYSY8MW5fYYWmZs2vNkKFMcfIdIhz3UBqLS9GkAeXrp GiXLxwL8xfUwtF683DivCM2Jv/QKe/3qDkoIw/fRZhGDWNRMe2irL80QQaGnnluG sqn21eK0iqzgoEBRUoOcw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrkeeigdekhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeffffffjeffudfggeevvdeitdetvdfgjefffeffjeel feejteevheeghffhvdfgleenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id EFBD11700089; Mon, 23 Oct 2023 11:16:46 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-1048-g9229b632c5-fm-20231019.001-g9229b632 MIME-Version: 1.0 Message-ID: <78a8a19a-2e00-4437-9c3a-dc069db77426@app.fastmail.com> In-Reply-To: <5be74be4-972f-4eae-ae46-6d92587ccc7f@processus.org> References: <5be74be4-972f-4eae-ae46-6d92587ccc7f@processus.org> Date: Mon, 23 Oct 2023 15:16:26 +0000 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Constructor promoted property and default value From: larry@garfieldtech.com ("Larry Garfield") On Mon, Oct 23, 2023, at 2:18 PM, Pierre wrote: > Hello internals, > > I stumbled upon this behavior, if I write this: > > ```php > class Foo > { > =C2=A0=C2=A0=C2=A0 public ?string $prop =3D null; > > =C2=A0=C2=A0=C2=A0 public function __construct(?string $prop =3D null) > =C2=A0=C2=A0=C2=A0 { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 $this->prop =3D $prop; > =C2=A0=C2=A0=C2=A0 } > } > > class Bar extends Foo > { > =C2=A0=C2=A0=C2=A0 public function __construct( > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 public ?string $bar =3D nu= ll, > =C2=A0=C2=A0=C2=A0 ) {} > } > > // 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 > { > =C2=A0=C2=A0=C2=A0 public function __construct( > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 public ?string $prop =3D n= ull, > =C2=A0=C2=A0=C2=A0 ) {} > } > ``` > > It won't work anymore and I have the following error: > > ``` > PHP Warning:=C2=A0 Uncaught Error: Typed property Foo::$prop must not = be=20 > accessed before initialization in php shell code:9 > ``` > > If I understand it correctly: > =C2=A0- in the first case, default value is attached to the object pr= operty,=20 > so if I omit its constructor, I have the default, > =C2=A0- in the second case, default value is attached to the construc= tor=20 > parameter, and not to the object property, which means that in case th= e=20 > parent constructor is not called in the `Bar` class, `$prop` remains=20 > initialized. > > It doesn't sound like a bug, but I think that many people would actual= ly=20 > expect otherwise: that the constructor promoted property keep their=20 > default even when constructor is not explicitly called. > > Is there any good reason behind this ? Wouldn't it be best to change=20 > this behavior ? Would it be a risk for backward compatibility (my gues= s=20 > is "not that much, probably not a all even") ? Where this becomes a problem is readonly properties, since those are not= allowed to have default values. (That would make them constants with w= orse performance.) A solution would need to be able to detect that the = parent::__construct() isn't called, and then call it anyway, or at least= partially call it. Unfortunately, I can think of many cases where such= a call would result in unexpected behavior. It might be possible to resolve, but it's definitely not simple, and it = could easily lead to weird behavior. --Larry Garfield