-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
1e3d5b8
to
52571b1
Compare
ee4e9f0
to
6bcafc6
Compare
There was a problem hiding this 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.
7522062
to
5ab999c
Compare
cd7f64b
to
4086975
Compare
@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? |
@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. |
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. |
f8e5092
to
2d1d16c
Compare
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
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.
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. :) |
2d1d16c
to
f8a297d
Compare
5368fa9
to
a990b91
Compare
a990b91
to
cba6a23
Compare
I'm going to merge this PR mid next week, unless there is any issue with the implementation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation looks fine.
RFC: https://wiki.php.net/rfc/readonly_classes PHP implementation: php/php-src#7305
05d94bc
to
c5efb74
Compare
> Readonly Classes > > Support for readonly on classes has been added. This adds a new sniff to detect this. Includes unit tests. Includes docs. Refs: * https://wiki.php.net/rfc/readonly_classes * https://www.php.net/manual/en/migration82.new-features.php#migration82.new-features.core.readonly-classes * https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.readonly * php/php-src#7305
> Readonly Classes > > Support for readonly on classes has been added. This adds a new sniff to detect this. Includes unit tests. Includes docs. Refs: * https://wiki.php.net/rfc/readonly_classes * https://www.php.net/manual/en/migration82.new-features.php#migration82.new-features.core.readonly-classes * https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.readonly * php/php-src#7305
RFC: https://wiki.php.net/rfc/readonly_classes PHP implementation: php/php-src#7305
RFC: https://wiki.php.net/rfc/readonly_classes