Re: Readonly properties - immutability by default
| From: | Eugene Sidelnyk | Date: | Sat, 17 Jul 2021 05:40:27 +0000 |
| Subject: | Re: Readonly properties - immutability by default | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-115447@lists.php.net to get a copy of this message | ||
> Having a "readonly" class where the properties are read-only by default
makes sense to me, but maybe the better way to do it is with an attribute?
We already have such an attribute provided. It is called [
#[Immutable]](
https://blog.jetbrains.com/phpstorm/2020/10/phpstorm-2020-3-eap-4/#immutable
).
If the intention is to add some logic for attributes, then unlikely it is
going to be accepted because attributes by definition are meta-data. It
doesn't add any logic for program.
On Sat, Jul 17, 2021 at 12:09 AM Mike Schinkel <mike@newclarity.net> wrote:
> > On Jul 16, 2021, at 6:12 AM, Bruce Weirdan <weirdan@gmail.com> wrote:
> >
> > On Fri, Jul 16, 2021 at 9:45 AM Eugene Sidelnyk <zsidelnik@gmail.com>
> wrote:
> >
> >> Readonly properties are really useful for DDD, where everything is
> going to
> >> be immutable. It promotes best practices. However for people to use it,
> >> syntax should be concise and brief.
> >
> > If every property of the class is readonly it would probably be better
> > to declare that with a class modifier.
> >
> > E.g.
> >
> > ```php
> > readonly class Entity {
> > public int $count;
> > public string $data;
> > }
> > ```
>
> Having a "readonly" class where the properties are read-only by default
> makes sense to me, but maybe the better way to do it is with an attribute?
>
> #[Readonly]
> class Entity {
> public int $count;
> public string $data;
> }
>
> OTOH, that would be inconsistent with using readonly as a
> keyword for
> properties.
>
> -Mike
> P.S. Maybe readonly properties should be implemented with an attribute
> instead of a keyword? But then that would be opening a can of worms...
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>