Re: [RFC] [DISCUSSION] Immutable/final/readonly properties
| From: | Marco Pivetta | Date: | Mon, 24 Feb 2020 08:25:18 +0000 |
| Subject: | Re: [RFC] [DISCUSSION] Immutable/final/readonly properties | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108735@lists.php.net to get a copy of this message | ||
On Mon, Feb 24, 2020, 09:19 Rowan Tommins <rowan.collins@gmail.com> wrote:
> On 24 February 2020 03:26:19 GMT+00:00, Marco Pivetta <ocramius@gmail.com>
> wrote:
> >The solution is trivial: don't use cloning:
> >
> >final class Foo
> >{
> > public function withBar($bar):self {
> > $instance = new self();
> > $instance->foo = $foo;
> > // more assignments here - unavoidable
> > return $instance;
> > }
> >}
>
>
> This works fine on paper, but is completely impractical as the class
> grows. Consider a class with 10 such properties
Yes, considered it: it's fine.
Also, the best approach would be to have a ctor with those 10 properties.
It's also impossible to use with inheritance, or to compose with traits (as
> Diactoros does, for instance)
Inheritance would be ditched in favour of even more composition, but even
then, inheritance is not a massive issue, especially when it comes to value
types.
Probably a couple hours of work to make a full PSR-7 implementation with
the old tests running, and the new approach applied 👍
For things that are more sensible, like a UUID, a Domain Event or a
Command, this is really a game changer anyway.
The good old Object Calisthenics rules do really apply.