[RFC] [Discussion] Readonly Classes

  116472
November 22, 2021 16:13 kocsismate90@gmail.com (=?UTF-8?B?TcOhdMOpIEtvY3Npcw==?=)
Hi Internals,

I'd like to propose adding support for readonly classes:
https://wiki.php.net/rfc/readonly_classes

The implementation heavily builds on the semantics of the already accepted
readonly properties RFC (https://wiki.php.net/rfc/readonly_properties_v2),
basically the only thing I had to implement in the PR are the following:
- Any declared property of a readonly class is implicitly treated as
readonly
- Creation of dynamic properties is forbidden

Readonly classes would make it easier to declare immutable(-alike) classes,
especially if a class
contains a lot of properties. As a side-effect, one could already opt out
from dynamic properties
completely, which won't really be possible for quite some time, even if
https://wiki.php.net/rfc/deprecate_dynamic_properties passes.

Regards:
Máté
  116473
November 22, 2021 16:53 contact@ricardoboss.de (Ricardo Boss)
Hi Máté,

you seem to have a typo in your RFC:

"
Similarly how overriding of readonly properties works, only a readonly
class can extend a readonly parent:

    class A {}
    readonly class B extends A {}
"

I think you meant to write `readonly class A {}` instead.

Apart from that, I think readonly classes would be a great idea!


Cheers!

Ricardo
  116474
November 22, 2021 17:10 michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=)
Hi Máté,

pon., 22 lis 2021 o 17:14 Máté Kocsis <kocsismate90@gmail.com> napisał(a):

> Hi Internals, > > I'd like to propose adding support for readonly classes: > https://wiki.php.net/rfc/readonly_classes
Did you forget to update the status? Cheers, Michał
  116476
November 22, 2021 17:47 michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=)
Hello Máté,

pon., 22 lis 2021 o 17:14 Máté Kocsis <kocsismate90@gmail.com> napisał(a):

> Hi Internals, > > I'd like to propose adding support for readonly classes: > https://wiki.php.net/rfc/readonly_classes > > The implementation heavily builds on the semantics of the already accepted > readonly properties RFC (https://wiki.php.net/rfc/readonly_properties_v2), > basically the only thing I had to implement in the PR are the following: > - Any declared property of a readonly class is implicitly treated as > readonly > - Creation of dynamic properties is forbidden >
Personally, I'm not convinced at all about this idea. The read-only property for me is a preliminary step before introducing full-blown property/field accessors. Instead of making the whole class readable I'd rather see sort of data classes similar to record classes in other languages where you get pass-by-value, built-in object initializer and class composition over inheritance like Go structs. But regardless of what I'd love to see this feature, I see no justified use case. Therefore I ask if there are any real use cases of it? and if there are other languages with similar functionality? Cheers, Michał Marcin Brzuchalski
  116477
November 22, 2021 17:52 larry@garfieldtech.com ("Larry Garfield")
On Mon, Nov 22, 2021, at 10:13 AM, Máté Kocsis wrote:
> Hi Internals, > > I'd like to propose adding support for readonly classes: > https://wiki.php.net/rfc/readonly_classes > > The implementation heavily builds on the semantics of the already accepted > readonly properties RFC (https://wiki.php.net/rfc/readonly_properties_v2), > basically the only thing I had to implement in the PR are the following: > - Any declared property of a readonly class is implicitly treated as > readonly > - Creation of dynamic properties is forbidden > > Readonly classes would make it easier to declare immutable(-alike) classes, > especially if a class > contains a lot of properties. As a side-effect, one could already opt out > from dynamic properties > completely, which won't really be possible for quite some time, even if > https://wiki.php.net/rfc/deprecate_dynamic_properties passes. > > Regards: > Máté
If I'm following, then this RFC is about 90% syntactic sugar for putting `readonly` on all properties, plus disabling dynamic properties. That's the long-and-short of it, yes? Does disabling dynamic properties offer any other advantages as have been discussed otherwise recently? (E.g., performance.) Is there a way to opt a given property back OUT of being readonly, or if you have a readonly class and need to add a single mutable property to it are you stuck adding `readonly` to all existing properties first? (I'm not sure I'm suggesting having such a mechanism, mostly just clarifying.) It's interesting that this also provides a backdoor way to force all properties to be typed, as a side effect. However... that also means you cannot mark a class readonly if any of its properties are callables, since callables cannot be typed. (I guess you could use mixed and then docblock callable, which is a bit fugly but not a problem introduced by this RFC.) --Larry Garfield