Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128020 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 6AB121A00BC for ; Sun, 13 Jul 2025 16:18:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1752423384; bh=ugGKcsQFpg4URQ4uU7vw7roPOeWg7Ia/7pwPMkP6xMU=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=d4WB938+hAloMLk4RHpyaztO7LRJIXc6Y24w+3DVdnEmJR4dR58HNpI26WmZe8N60 l1Gv9VBU8OlyK47lMq+MZOxI8tKmTEqAp+dzj5KH+uiSKtkTEZs9d3pbpy2l68bCQi a0GYS/3OYLq7Fc1JLD2FnUwuVeIUPzwlrO7mlbtnTTYlMzOCmtlyQodrDwzPpEO4rD aKrzp4WTG+HZF2zk2F+eXdXQRUIS2xvr2WXkW0nSilnXn8qdNUJciceJlnw9T9xUeE RY8TeJ2tRRrerLCk9tDxsSPCwuuiL8nC3mKd6BSm8boRWIB0foDhnt2pIocApfKr9e 4QSIcBu4wP4zA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8279618005D for ; Sun, 13 Jul 2025 16:16:22 +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.7 required=5.0 tests=BAYES_05,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 ; Sun, 13 Jul 2025 16:16:21 +0000 (UTC) Received: from avril.gn2.hosting (localhost [127.0.0.1]) by avril.gn2.hosting (Postfix) with ESMTP id 86E2E1C40C1D; Sun, 13 Jul 2025 18:18:08 +0200 (CEST) Received: from smtpclient.apple (unknown [111.68.29.103]) by avril.gn2.hosting (Postfix) with ESMTPSA id 549B31C4082C; Sun, 13 Jul 2025 18:18:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nicksdot.dev; s=default; t=1752423488; 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=Aj2ZUJS1TegrcNEUS+UgYOToThmjuf4/6OtVfivjsok=; b=NnYHXuIrG8tP8nPcIJugBdgS9Mm1xRiiWW89Bhi2fOtdj+eJFy1gwF2ZRVd35S1X0KDGql SJCLoq710dE7+Q8jJOiZofBfKUL4XUb/5XDRDA8GbMePmk2UCqs5FJVOb0YiAgSXo2LxFm ZV0377Lz7j4BUN5v/lN6t18iQ9Uj11ZQGm2VvHVwSShzn10o2KME1FSHTTmskkQeeiFAfi 12do0O5gH5ihA1Zh0cGF/+Z5H3DF6ysMFrEB5+1IQ+1O6vfGDY3RqWDc/GKF25++ZqccE9 REKRLREQm6lSO9cqx+ZPokiiIHtWpMnu6vmJnS0rYA/YTrLszKqdGVi1sVfJZg== Message-ID: <71E5E8FE-605D-4B32-8CCD-F7ECBD454E24@nicksdot.dev> Content-Type: multipart/alternative; boundary="Apple-Mail=_61CC82D1-1048-4B0B-9830-4A8103FF1A00" 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: Sun, 13 Jul 2025 23:17:51 +0700 In-Reply-To: Cc: internals@lists.php.net To: Marc Bennewitz References: <1e8634d7-ac1a-4025-b4e2-1948aabf5251@app.fastmail.com> <9D5043B2-1589-4FD5-B289-6E98FB1177BE@nicksdot.dev> <16BD443D-A179-465D-84A0-6E3780F62D8E@gmail.com> <203C1BD7-E688-4E26-8EA6-EA5331525470@nicksdot.dev> X-Mailer: Apple Mail (2.3826.600.51.1.1) From: php@nicksdot.dev (Nick) --Apple-Mail=_61CC82D1-1048-4B0B-9830-4A8103FF1A00 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On 13. Jul 2025, at 20:38, Marc Bennewitz wrote: > Hi Nick, Claude, Hey Marc, > I think it's important to explicitly mark it as "this value will be = stored in memory", I mean just silently caching the get hook could = quickly lead to unexpected behavior. Like one would expect the value to = be changed >=20 The most here made the argument that "a changing value from a readonly = get hook" would be the unexpected behaviour.=20 That=E2=80=99s why we ended up with the current =E2=80=9Calternative = implementation 2=E2=80=9D. Please see my last answer to Rob for a fair = example [1] . The current preferred alternative implementation covers both = situations... If a property is `readonly`:=20 - you can set once - on read you always get the same (once computed) value back If a property is NOT `readonly`: - you can set often - on read you always get the fresh (often computed) value back I argue that this is a very easy mental model. I hope that voters agree on =E2=80=9Ccached may be implied by = readonly=E2=80=9D, as Claude called it. > and another one wonders why a big chunk of memory will not be freed = `get =3D> $this->readBigFile();`. >=20 Where do you see non-freed memory in one but not the other?=20 - in both scenarios, readonly or not, the `readBigFile()` will end up in = memory. - on each consecutive property call the usage in both scenarios is the = same. - when assigning a call the same property to multiple tmp vars the = cached, once-computed version uses less memory than the non-cached = version Do I miss something? Did I misunderstand something? Additionally, the cached version has the benefit that the expensive = computation only happens once. > On the one hand I like the cached modifier but personally I would = prefer a separate init hook because it seems to be more clear that this = is a backed property that will be initialized once. >=20 To have an `init` hook doesn=E2=80=99t solve the get hook issue. > The cached modifier I would expect to be an attribute applicable to = any function which uses another cache store similar to how it's possible = in python to memorize function calls which would be a very different = feature. >=20 As earlier answered to Claude [2], I seek to write less code. To = introduce a `cached` modifier voids this for no strong reason (please = see =E2=80=9Cmental model=E2=80=9D above). =20 -- Cheers, Nick [1] https://news-web.php.net/php.internals/128010=20 [2] https://news-web.php.net/php.internals/128007 --Apple-Mail=_61CC82D1-1048-4B0B-9830-4A8103FF1A00 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On 13. Jul = 2025, at 20:38, Marc Bennewitz <marc@mabe.berlin> = wrote:

Hi Nick, = Claude,

Hey Marc,
=

I = think it's important to explicitly mark it as "this value will be stored in memory", I mean just silently caching the get hook could quickly lead to unexpected behavior. Like one would expect the value to be changed

The = most here made the argument that "a changing value from a readonly get = hook" would be the unexpected behaviour. 
That=E2=80=99s = why we ended up with the current =E2=80=9Calternative implementation = 2=E2=80=9D. Please see my last answer to Rob for a fair example [1] = .

The current preferred alternative = implementation covers both = situations...

If a property is = `readonly`: 
- you can set once
- = on read you always get the same (once computed) value = back

If a property is NOT = `readonly`:
- you can set often
- on = read you always get the fresh (often computed) value = back

I argue that this is a very easy = mental model.
I hope that voters agree on =E2=80=9Ccached may be implied by readonly=E2=80=9D, as Claude called = it.

and = another one wonders why a big chunk of memory will not be freed `get =3D> = $this->readBigFile();`.

Where do you = see non-freed memory in one but not the = other? 

- in both scenarios, readonly or = not, the `readBigFile()` will end up in memory.
- on each = consecutive property call the usage in both scenarios is the = same.
- when assigning a call the same property to multiple = tmp vars the cached, once-computed version uses less memory than the = non-cached version

Do I miss something? Did I = misunderstand something?

Additionally, the = cached version has the benefit that the expensive computation only = happens once.

On the one hand I like the cached modifier but personally I = would prefer a separate init hook because it seems to be more clear that this is a backed property that will be initialized = once.

To have an `init` hook doesn=E2=80=99= t solve the get hook issue.

The cached = modifier I would expect to be an attribute applicable to any function which uses another cache store similar to how it's possible in python to memorize function calls which would be a very different feature.

As earlier answered to = Claude [2], I seek to write less code. To introduce a `cached` modifier = voids this for no strong reason (please see =E2=80=9Cmental model=E2=80=9D= = above).
 
--

Cheers,<= /b>
Nick

[1] = https://news-web.php.net/php.internals/128010 
[2] = https://news-web.php.net/php.internals/128007

<= /html>= --Apple-Mail=_61CC82D1-1048-4B0B-9830-4A8103FF1A00--