Re: [RFC] Readonly property hooks
| From: | Rob Landers | Date: | Sat, 19 Jul 2025 10:12:40 +0000 |
| Subject: | Re: [RFC] Readonly property hooks | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128134@lists.php.net to get a copy of this message | ||
On Sat, Jul 19, 2025, at 12:09, Claude Pache wrote:
>
>
>> Le 19 juil. 2025 à 09:46, Rob Landers <rob@bottled.codes> a écrit :
>>
>>
>>
>> On Sat, Jul 19, 2025, at 03:04, Claude Pache wrote:
>>>
>>>
>>>
>>>> Le 19 juil. 2025 à 00:41, Rob Landers <rob@bottled.codes> a écrit :
>>>>
>>>> The original author (Nikita) suggested that there's nothing in the original
>>>> design that precludes accessors -- and highlights languages where there are both and they are doing
>>>> just fine more than 5 years later.
>>>>
>>>
>>> Hi Rob,
>>>
>>> It is indeed entirely reasonable to have both readonly properties and hooked properties
>>> (aka accessors), and today PHP has indeed both of them (and even asymmetric visibility on top of
>>> that, as a separate feature contrarily to C#). But it doesn’t mean that it is reasonable for the
>>> *same* property to be *both* readonly and hooked, which is the point that is currently disputed. —
>>> What do the other languages allow? Is it possible to define a readonly property with a user-defined
>>> getter? (Disclaimer: Even if one of them allows such a thing, I’ll still think that it is a bad
>>> idea.)
>>>
>>> —Claude
>>
>> Hey Claude,
>>
>> From what I've seen in other languages, this combination is fairly common and not
>> inherently poblematic.
>>
>> - C# allows get hooks with user-defined logic, even in readonly structs.
>> - Kotlin uses val with a get() body, which is readonly from the consumer's
>> perspective, even though the value is computed.
>> - TypeScript allows a get-only accessor which acts readonly.
>> - Swift also allows get-only computed accessors/hooks.
>
> Hi Rob,
>
> The main problem is that we don’t agree on the meaning of “readonly property”.
>
> I’ve check TypeScript:
>
> * It has getters and setters, which correspond to PHP get/set hooks without backing store.
>
> * Separately, it also has a
readonly modifier that can be
> applied to properties; the semantics is that such a property may be initialised either at
> declaration or inside the constructor, but cannot be modified afterwards. That corresponds
> approximatively to PHP readonly properties.
>
> * But a “get-only accessor” is not the same thing as a “readonly property” in the
> specific sense of ”a property decorated with the readonly
> modifier”. Also, you cannot add the readonly modifier to a get accessor.
>
> —Claude
>
The error you get when trying to modify the "get-only accessor" is Error: Cannot
assign to 'name' because it is a read-only property
Thus, readonly is implied by the get-only accessor, there's no need to specify it directly.
— Rob