On Tue, Jun 29, 2021, at 8:08 AM, Nikita Popov wrote: > > You might not like the boilerplate, but that just works. > > > > Can this be considered Nikita? > > > > Well, it's a nifty hack :) I don't think this is the solution we want to > encourage though. It requires you pass extra information through a > side-channel -- I think I'd rather not use readonly than write that code. > > Continuing along the same line, one could extend that to "clone with > argument" and do something like this: > > public function __clone(self $original, array $replacements = []) { > foreach ($original as $k => $v) { > $this->$k = $replacements[$k] ?? $original->$k; > } > } > > and then do "clone $this(['bar' => $bar])". > > In any case, I don't want to include changes to cloning in this proposal -- > the topic is related, but also orthogonal to readonly properties. > Unfortunately, it will not be possible to get cloning changes into PHP 8.1 > anymore, due to feature freeze. > > It's okay to vote against this if cloning is a deal breaker. In that case > I'll probably either work on cloning before re-proposing this, or pivot to > asymmetric visibility -- it's not my first preference, but it may be the > more pragmatic choice. Cloning is definitely the weak point of this > proposal. > > Regards, > Nikita
I already went through the clone-arguments mental exercise in my earlier analysis, and the code it produces is totally disgusting. :-) clone-with is a considerably better approach if you're starting from readonly. (It's also better if you start from asymmetric visibility, although that version needs a clone-help feature far less.) --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php