Re: [RFC] [DISCUSSION] Immutable/final/readonly properties

From: Date: Mon, 24 Feb 2020 08:18:54 +0000
Subject: Re: [RFC] [DISCUSSION] Immutable/final/readonly properties
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-108734@lists.php.net to get a copy of this message
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, each with their own with* method: switching from clone to explicit assignments means adding 90 lines of code, all of it copy-and-pasted boilerplate that's hard to spot mistakes in. It's also impossible to use with inheritance, or to compose with traits (as Diactoros does, for instance), because every with* method needs to know the full details of how to create a partial clone. Regards, -- Rowan Tommins [IMSoP]

« previous php.internals (#108734) next »