# [RFC] [Discussion] Readonly Classes

- Thread: https://externals.io/message/116472
- Original archive: https://news-web.php.net/php.internals/116472
- Started by kocsismate90@gmail.com on 2021-11-22T16:13:49+00:00
- Messages: 9
- RFC: Readonly classes (Implemented (PHP 8.2)) — https://externals.io/rfc/readonly_classes.md
- RFC: Deprecate dynamic properties (Implemented (PHP 8.2)) — https://externals.io/rfc/deprecate_dynamic_properties.md
- RFC: Readonly properties 2.0 (Implemented (PHP 8.1)) — https://externals.io/rfc/readonly_properties_v2.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #116472 — kocsismate90@gmail.com — 2021-11-22T16:13:49+00:00

URL: https://externals.io/message/116472#116472

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 — Ricardo Boss — 2021-11-22T16:53:35+00:00

URL: https://externals.io/message/116472#116473 · In reply to: #116472

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 — michal.brzuchalski@gmail.com — 2021-11-22T17:10:57+00:00

URL: https://externals.io/message/116472#116474 · In reply to: #116472

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 — michal.brzuchalski@gmail.com — 2021-11-22T17:47:29+00:00

URL: https://externals.io/message/116472#116476 · In reply to: #116472

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 — Larry Garfield — 2021-11-22T17:52:10+00:00

URL: https://externals.io/message/116472#116477 · In reply to: #116472

> 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

---

## #117540 — kocsismate90@gmail.com — 2022-04-19T07:58:57+00:00

URL: https://externals.io/message/116472#117540 · In reply to: #116477

Hi Everyone,

After quite a long pause, I'd like to revive the discussion of readonly
classes and possibly put it to vote in the coming week(s).

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?
>

Yes, exactly!


> Does disabling dynamic properties offer any other advantages as have been
> discussed otherwise recently?  (E.g., performance.)
>

No, I'm not aware of any other advantages. The only reason why I included
this feature into the current RFC is because otherwise a readonly class
wouldn't be readonly. :)
The same applies for forbidding the declaration of static properties.

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.)
>

Yes, you are right. This is an inconvenient situation for sure, but neither
I can imagine any other way to back a property out of being readonly than
to remove
the readonly class modifier + add readonly property modifier where
possible. Of course, the readonly property modifier doesn't apply to the
internal state of objects...
so doing the above is not necessary if you have to modify a readonly
property which is an object.


> 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.)


Yeah, you are right, an ugly hack is to use the mixed type for properties
that cannot be typed otherwise (e.g. resource). I believe in case of
callables, one could use
the Closure type instead, especially since the first-class callable syntax
has been introduced.

I hope I managed to address your questions/concerns.

Máté

---

## #117588 — kocsismate90@gmail.com — 2022-04-25T06:29:52+00:00

URL: https://externals.io/message/116472#117588 · In reply to: #116472

Hi Internals,

As there's not much discussion, I will start voting on Wednesday.

Cheers,
Máté

---

## #117599 — Thomas Gutbier — 2022-04-25T14:51:54+00:00

URL: https://externals.io/message/116472#117599 · In reply to: #117540

Am 19.04.2022 um 09:58 schrieb Máté Kocsis:
> Hi Everyone,
>
> After quite a long pause, I'd like to revive the discussion of readonly
> classes and possibly put it to vote in the coming week(s).
>
> If I'm following, then this RFC is about 90% syntactic sugar for putting
> > […]

Should this rfc (https://wiki.php.net/rfc/deprecate_dynamic_properties) 
be considered here?

The attribute #[AllowDynamicProperties] should not be allowed for 
readonly classes.

Thomas

---

## #117603 — kocsismate90@gmail.com — 2022-04-25T18:07:23+00:00

URL: https://externals.io/message/116472#117603 · In reply to: #117599

Hi Thomas,

The attribute #[AllowDynamicProperties] should not be allowed for
> readonly classes.
>

 Nice catch and I do agree with you, so I will add this restriction.

Máté
