Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128001 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 lists.php.net (Postfix) with ESMTPS id AE19A1A00BC for ; Fri, 11 Jul 2025 04:21:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1752207568; bh=iYEHd1n0YJXwWu2ejuhptYTXVpYlo5QvwmgEPtMjQ4Y=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=int1ponn+waBpTjLyUSOwuiAdqhcG0dU904iqZj3HdrjGKJUn3FLZfuNB1B8HQhkE trjX9H4Sz/Rl2Mf+ZJphRuLWSVysoMpnHM5kRqZfEAkJv4GrGFrsMPvXP/NcpiK/Sc H3War61dlC4WpGJXa+g3GeIb29ar9SFjcSbrXLPMizlwJtk4yASVochbuvF92cxooU flPpZa4vD+mSEBwoEo33X+3FsoxtaAdKfMqd8Zg5pOehG78FLykM6pr6huy/xhafPi amD58D0d9fAY2G5jvK5swKOvBRS1KiFW+BtJbfYrTD8FV1t18CUpvYjm4GxOu3aUBq 0VCn11o7CdEdw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5A49D180082 for ; Fri, 11 Jul 2025 04:19:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,HTML_MESSAGE, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from avril.gn2.hosting (avril.gn2.hosting [84.19.162.247]) (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, 11 Jul 2025 04:19:26 +0000 (UTC) Received: from avril.gn2.hosting (localhost [127.0.0.1]) by avril.gn2.hosting (Postfix) with ESMTP id C38641C40816; Fri, 11 Jul 2025 06:21:13 +0200 (CEST) Received: from smtpclient.apple (unknown [111.68.29.103]) by avril.gn2.hosting (Postfix) with ESMTPSA id A066B1C405F1; Fri, 11 Jul 2025 06:21:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nicksdot.dev; s=default; t=1752207673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+Ma9X1suq9+2FWX00pldMEpXqbA4qH2BNXap99imzx4=; b=LR3YMnHtkMm0e7OfkwiJ0czh5tZ0itAb+oaLpekAc0bfBYkfNIKBJVPh7U5bL/VWqsiXJ1 +pV+xd2Xbr47hZ3gI1bhSvJKDyVc5C5NAW2JoEyg1ZO6R0rFVzC0V+rKeTbmykSF6KKKwC np89AMsscxyvjfkgxVjmBhxwFLrsW+iiwM21ka3KiQbZ28Wo9KFSpaGwFA6yr/tLvHBJIR bAKXZrqdESX525tQhr7Aa+mAvNKLeEswzaPnuZ9Xj0N+qygIY2JWDozVlNdagJL2HR+WZG P8281UAvspCmpnoJSX2sFWJNkt/20geL/KGBZPKzXqrDGvdt0joKDsXZNvZssA== Message-ID: <13B58381-AA61-4D38-A688-DD9E367ADE6F@nicksdot.dev> Content-Type: multipart/alternative; boundary="Apple-Mail=_C7FF9989-5B1F-4DB9-968A-B3B53BB5A937" Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.600.51.1.1\)) Subject: Re: [PHP-DEV] [RFC] Readonly property hooks Date: Fri, 11 Jul 2025 11:20:57 +0700 In-Reply-To: Cc: Larry Garfield , php internals To: =?utf-8?Q?Tim_D=C3=BCsterhus?= References: <1e8634d7-ac1a-4025-b4e2-1948aabf5251@app.fastmail.com> X-Mailer: Apple Mail (2.3826.600.51.1.1) From: php@nicksdot.dev (Nick) --Apple-Mail=_C7FF9989-5B1F-4DB9-968A-B3B53BB5A937 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hey Tim, > On 10. Jul 2025, at 17:37, Tim D=C3=BCsterhus = wrote: >=20 > Hi >=20 > Am 2025-07-08 17:10, schrieb Larry Garfield: >> The only way to make the readonliness fully guaranteed would be to = force a readonly property to be cached >=20 > Or by not allowing a `get` hook on readonly properties, of course. >=20 > Best regards > Tim D=C3=BCsterhus Personally, I would really like to have `get` hooks on readonly = properties. Please consider something like this: ```php readonly class Foo { public function __construct( public Output $style, public string $some { get =3D> Output::One =3D=3D=3D $this->style ? = ucfirst($this->some) : strtoupper($this->some); set =3D> '' !=3D=3D $value ? $value : throw new = \Exception(); } ) {} } ``` Easy-to-digest one-liners. Concerns remain separated. Set takes care of = validation, get formats. If `get` would not be allowed, we couldn=E2=80=99t do such an obvious = thing. For what reason? Instead we would need to delegate formatting to the `set` hook which is = messy. ```php readonly class Foo { public function __construct( public Output $style, public string $some { set =3D> '' !=3D=3D $value ? (Output::One =3D=3D=3D = $this->style ? ucfirst($value) : strtoupper($value)) : throw new = \Exception(); } ) {} } ``` Now that I have proposed alternative implementations with caching, I = don=E2=80=99t see why `get` should not be allowed. Cheers, Nick Aside: I added two links to alternative implementations to the PR = description.= --Apple-Mail=_C7FF9989-5B1F-4DB9-968A-B3B53BB5A937 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Hey Tim,

On 10. Jul 2025, at 17:37, Tim D=C3=BCsterhus = <tim@bastelstu.be> wrote:

Hi

Am 2025-07-08 = 17:10, schrieb Larry Garfield:
The only way = to make the readonliness fully guaranteed would be to force a readonly = property to be cached

Or by not allowing a `get` = hook on readonly properties, of course.

Best regards
Tim = D=C3=BCsterhus

Personally, I = would really like to have `get` hooks on readonly properties. Please = consider something like = this:

```php
readonly class Foo
{
public function __construct(
public Output $style,
= public string $some = {
get =3D> Output::One =3D=3D=3D $this->style ? ucfirst($this->some) : strtoupper($this->some);
= set =3D> '' !=3D=3D $value ? = $value : throw new \Exception();
= }
) = {}
}
```

Easy-to-digest one-liners. Concerns remain separated. Set takes = care of validation, get formats.
If `get` would not be = allowed, we couldn=E2=80=99t do such an obvious thing. For what = reason?
Instead we would need to delegate formatting to the = `set` hook which is = messy.

```php
readonly class Foo
{
public function __construct(
public Output $style,
= public string $some = {
set =3D> '' !=3D=3D = $value ? (Output::One =3D=3D=3D $this->style ? ucfirst($value) : strtoupper($value)) : throw new \Exception();
= }
) = {}
}
```

Now= that I have proposed alternative implementations with caching, I = don=E2=80=99t see why `get` should not be = allowed.

Cheers,
Nick
=
Aside: I added two links to alternative implementations = to the PR description.
= --Apple-Mail=_C7FF9989-5B1F-4DB9-968A-B3B53BB5A937--