Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123537 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id C70721A009C for ; Fri, 7 Jun 2024 13:11:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1717765944; bh=H3YzbJPUnyrtjl2vSQcqiv19N2tuVdg4iZlZ3Th4uEA=; h=In-Reply-To:References:Date:From:To:Subject:From; b=GCV59wO+2KDqI8cNdU4x4jgN/IfVtx56spgOsm/CCl5ZIngQJBNUYFNsXr3KdRVX8 bLbicObT62O68XzrazTNQsnf5olh7FxL2FBSLUV7mfxRereAzuqaqUwQX3JZAcVTQD uWXgdh4oSiyaPdNjFXgOzVlnsIMRADHLrWRTE/0f9mxFpaQd9NeRmGbUveGp6Uffqa 0fvVWzwC7XRTDFv7gj0OO1lBkGyWUSeA5W2GVrTnM6nqrdjArFO5DZ+AwuUWIUrEEh jbQfyfidLNwBXmcz4pkoDcilBPdzJ2sm4+wGXT3MN0oghLerslx7GgMelLUJc4+liM 46PU4BhRpNjVQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E9E151805C9 for ; Fri, 7 Jun 2024 13:12:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from wfhigh4-smtp.messagingengine.com (wfhigh4-smtp.messagingengine.com [64.147.123.155]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 7 Jun 2024 13:12:22 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.west.internal (Postfix) with ESMTP id D62901800099 for ; Fri, 7 Jun 2024 09:11:14 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Fri, 07 Jun 2024 09:11:14 -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:subject:subject:to :to; s=fm1; t=1717765874; x=1717852274; bh=YZI0oJKW6inOEm9Buu9/V xgweVIe7R9wjJAlerU07s0=; b=cQ0trYuHkRywJtTzfmqmZdcPhB1W7vij2AELo +ycYuYVYOKpSyPRcjFwTKQ+P7q+p9UBlYRrCUYcI1dub6vgCQjMPMgiDeaLgy9J1 C6w9JI1gqE2EEhe+u+uvsKeHdhIYuN/h0U/tVGVyz93hGxlKn7JE9gIKpF4KY/mw 3YvF+gyqSHgonqLfRjQF/QzpGW10HNaHnrwNgkE8hFMST/JLIL/HdoUXIJCPM9GL rlXR1MYuL23i/mhSlV1/oj1PBMAPbQ3Ibb7FiG5F80g8kyAYqmg8F33KS6covfZb /j4jKYqgJytLPfmwT+aoL59fIxnofkL44+PrgXU54nOKT7seg== 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:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1717765874; x= 1717852274; bh=YZI0oJKW6inOEm9Buu9/VxgweVIe7R9wjJAlerU07s0=; b=O D0Qd/VNmvSfPMqMnne9i71q4TUMXrJhqokUt/V+qZqXaX5sfYuPadRmikx40Ow99 EnmoOmB9pGYGTW6e9boKb6FRk/uT8sQNzpvLrVChiHZkXROlBtjko90VOqQk+21g QDKkSj3K58wCAi0sm7SPIvM+qHBJ4mnG2SlBuiaaTrSWTzaBnd/YpqP6qe5/tXzR ZHIJLAYeFfNudcdj68ZBYPEEvysgfPANepnl8k8Ge4BQwTFWhrNqvcm7jHHrJpwt p7wWs9jYssrU8tgnqp/p3dRovYlD8ZvAL0MHOOaBIhlIUnd9EKFTI5HUVhW9gKED MnGZmWk1BoRfJ0mZ0BEtg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedtuddgfeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgeeghefgteejheeggfeghfelueeggfdtjeeivedv tefhveeguedufeelhedvteeinecuffhomhgrihhnpehphhhprdhnvghtnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhf ihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 0B9191700093; Fri, 7 Jun 2024 09:11:13 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-497-g97f96844c-fm-20240526.001-g97f96844 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <9c53585a-7898-4406-87ba-8b2055c14904@app.fastmail.com> In-Reply-To: References: Date: Fri, 07 Jun 2024 13:10:53 +0000 To: "php internals" Subject: Re: [PHP-DEV] Default value for readonly properties Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Fri, Jun 7, 2024, at 9:07 AM, Claude Pache wrote: > Hi, > > As of today, readonly properties cannot have a default value: > > ```php > class UltimateQuestion { > readonly int $answer =3D 42; // Fatal error: Readonly property=20 > Question::$answer cannot have default value > } > ``` > > The rationale given in the original RFC=20 > (https://wiki.php.net/rfc/readonly_properties_v2) was: > >> As the default value counts as an initializing assignment, a readonly= property with a default value is essentially the same as a constant, an= d thus not particularly useful. The notion could become more useful in t= he future, if new expressions are allowed as property default values. At= the same time, depending on how exactly property initialization would w= ork in that case, having a default value on a readonly property could pr= eclude userland serialization libraries from working, as they would not = be able to replace the default-constructed object. Whether or not this i= s a concern depends on whether the property is initialized at time of ob= ject creation, or as an implicit part of the constructor (or similar). A= s these are open questions, the conservative choice is to forbid default= values until these questions are resolved. >>=20 > A new fact is that properties will be able to be declared in interface=20 > since PHP 8.4 (as introduced in the recently accepted property hooks=20 > RFC). It is true that, for an individual class, a readonly property is=20 > functionally equivalent to a constant. But my class implements an=20 > interface (or, before 8.4, follows a non-written protocol), and whethe= r=20 > that particular implementation choose to always hold the same value fo= r=20 > a specific property is irrelevant. > > > ```php > interface Question { > public int $answer { get; } > } > class UltimateQuestion implements Question { > readonly int $answer =3D 42; > } > ``` > > Therefore I think it is reasonable for readonly properties to accept=20 > default values. > > About the concern given in the original RFC: =E2=80=9Chaving a default= value on=20 > a readonly property could preclude userland serialization libraries=20 > from working, as they would not be able to replace the=20 > default-constructed object=E2=80=9D... TBH, as I almost never serialis= e my=20 > objects, and even less often customise serialisation, I can only guess=20 > what the effective issue is. My best guess is that a custom=20 > `__unserialize()` method would not be able to reinstate the=20 > incriminated readonly properties, because those would already have bee= n=20 > initialised with the default value. If this is the case, a solution is=20 > to do for `__uninitialize()` the same thing that we have done for=20 > `__clone()`, see:=20 > https://wiki.php.net/rfc/readonly_amendments#proposal_2readonly_proper= ties_can_be_reinitialized_during_cloning > > =E2=80=94Claude It's also something I'd have to work around in Serde, and other user-spa= ce serializers would have to as well. So it would mean work for seriali= zers, not all of which use __unserialize(). Also, if aviz passes, a public private(set) property has no such restric= tion and can serve virtually the same role without any of the limitation= s of readonly. Hooks also offer another workaround already: class UltimateQuestion implements Question { public int $answer { get =3D> 42) } That would have the same net result (at a very small performance hit for= the hook invocation), without any compilation issues. And it doesn't g= ive serializers any more work to do than they already will have to do fo= r hooks and virtual properties. So while I agree that limitation is annoying, I think there are enough w= ays around it now and hopefully soon if aviz passes that it's more troub= le than it's worth to try and address directly. --Larry Garfield