> 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’s why we ended up with the current “alternative implementation 2”. 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 “cached may be implied by readonly”, as Claude called it.
> and another one wonders why a big chunk of memory will not be freed get =>
> $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’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 “mental model” above).
--
Cheers,
Nick
[1] https://news-web.php.net/php.internals/128010
[2] https://news-web.php.net/php.internals/128007