Skip to content

Add support for readonly classes #7305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 16, 2022
Merged

Conversation

kocsismate
Copy link
Member

@kocsismate kocsismate commented Jul 25, 2021

@kocsismate kocsismate added the RFC label Jul 25, 2021
@kocsismate kocsismate added this to the PHP 8.2 milestone Jul 25, 2021
@kocsismate kocsismate force-pushed the readonly-class branch 2 times, most recently from 1e3d5b8 to 52571b1 Compare July 25, 2021 13:34
@kocsismate kocsismate force-pushed the readonly-class branch 2 times, most recently from ee4e9f0 to 6bcafc6 Compare July 26, 2021 07:55
Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me.

@kocsismate kocsismate force-pushed the readonly-class branch 4 times, most recently from 7522062 to 5ab999c Compare July 27, 2021 13:43
@kocsismate kocsismate force-pushed the readonly-class branch 2 times, most recently from cd7f64b to 4086975 Compare August 5, 2021 09:22
@kocsismate
Copy link
Member Author

kocsismate commented Nov 17, 2021

@nikic Do you have any suggestion regarding static properties? I'm unsure if readonly classes should be forbidden to declare them or they should be allowed?

@nikic
Copy link
Member

nikic commented Nov 17, 2021

@kocsismate I'd say forbidden. In the future, we may want to support readonly static properties in the future (they mainly weren't originally included for technical reasons -- too much effort for little practical use) in which case one would expect that a static property on a readonly class would also be readonly. If we allow them now though, that wouldn't be the case.

@kocsismate
Copy link
Member Author

I'd say forbidden. In the future, we may want to support readonly static properties in the future (they mainly weren't originally included for technical reasons -- too much effort for little practical use) in which case one would expect that a static property on a readonly class would also be readonly. If we allow them now though, that wouldn't be the case

Thanks for the answer! I was also slightly towards making the rules stricter now so that the restrictions can be lifted afterwards, but wasn't exactly sure. I'll update the implementation, + the RFC, and start discussion a bit later.

@kocsismate kocsismate force-pushed the readonly-class branch 2 times, most recently from f8e5092 to 2d1d16c Compare November 22, 2021 13:41
@amcsi
Copy link

amcsi commented Nov 23, 2021

Is the purpose of this really just to have a shorthand for all properties being readonly, or is this meant to enable true immutable strictures? Because if it'd the latter, then perhaps it could be taken a step further and only allow for instances of readonly classes for object properties. Maybe even disallow resources, and disallow being able to create (mutable) references to properties if we want to go real strict with immutability.

@kocsismate
Copy link
Member Author

Is the purpose of this really just to have a shorthand for all properties being readonly, or is this meant to enable true immutable strictures? Because if it'd the latter, then perhaps it could be taken a step further and only allow for instances of readonly classes for object properties. Maybe even disallow resources, and disallow being able to create (mutable) references to properties if we want to go real strict with immutability.

The purpose is to offer a shorthand for readonly properties. I think it's a very light and flexible way to add support for immutable-alike classes. If someone wants to make one step ahead then an immutable keyword would be the best way forward. But I see the value in a readonly class modifier as well.

Because if it'd the latter, then perhaps it could be taken a step further and only allow for instances of readonly classes for object properties.

It sounds like a good idea for me at first, but doing so would create an inconsinstency: readonly properties can be non-readonly classes, while readonly classes cannot include non-readonly class properties.

Maybe even disallow resources,

There's no resource type, so it's not feasible to do.

If you'd like to continue the discussion, please use the mailing list instead. :)

@kocsismate
Copy link
Member Author

I'm going to merge this PR mid next week, unless there is any issue with the implementation.

Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks fine.

nikic pushed a commit to nikic/PHP-Parser that referenced this pull request May 15, 2022
@kocsismate kocsismate merged commit 7850c10 into php:master May 16, 2022
@kocsismate kocsismate deleted the readonly-class branch May 16, 2022 18:40
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Dec 25, 2022
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Dec 27, 2022
devengineerikrime pushed a commit to devengineerikrime/PHP-Parser that referenced this pull request Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants