Re: Allow hooks in `readonly` promoted properties
On Mon, Jun 2, 2025, at 11:34 PM, Nick wrote:
> I would like to add, personally, I don’t believe the above is dumb:
>
> ```php
> class Dumb {
> public readonly int $value { get => $this->value * random_int(1, 100); }
> }
> ```
>
> I think it is a legitimate use-case.
> Why wouldn’t a readonly
property allow us to format, cast or however
> mutate a “only once written value” on consumption? It will not change
> the underlying value.
> If it makes things easier for us, allow it. It’s not like this is some
> hidden implicit behaviour. We consciously must add the extra code,
> hence expect the output to be changed accordingly.
It's about expectation setting. If you see a property marked readonly
, it's
reasonable to expect this to be true:
$foo->bar == $foo->bar;
For a traditional field (pre-hooks), this would be trivially true. With hooks, it may or may not
be. Saying "well, that assumption doesn't hold anymore, deal" is certainly an
option, but it's not an option we wanted to pursue as part of the larger RFC. But that is
certainly a direction we could take.
> So, I would love to see this RFC to be implemented.
> Maybe you want to move it to discussion? Then my separate thread here
> would be obsolete.
I believe at the moment that RFC text is all there is. :-) I don't know that it's worth
opening a discussion without at least a mostly-done implementation. Also, Ilija is rather busy on
other tasks at the moment, as am I. (Unless someone else wants to jump in to implement it, which
would be fine.)
--Larry Garfield
Thread (14 messages)