On 24 February 2020 03:26:19 GMT+00:00, Marco Pivetta <ocram...@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] -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php