> On 9 Jun 2023, at 07:11, Larry Garfield <la...@garfieldtech.com> wrote:
> 
> On Thu, Jun 8, 2023, at 5:39 PM, Stephen Reay wrote:
> 
>> Is there a specific reason `clone with (foo: $bar);` can’t simply pass 
>> the arguments given to with(), to the __clone() magic method?
>> 
>> It leaves the developer free to use the passed argument(s) or deep 
>> clone existing properties or a mix depending on what’s passed, and 
>> seems like it has the least “magic” or unknown behaviour in terms of 
>> when things happen.
>> 
>> Just a thought.  
> 
> I experimented with that a few years back.  The results showed it is actually 
> pretty awful in practice.
> 
> https://peakd.com/hive-168588/@crell/object-properties-part-2-examples
> 
> --Larry Garfield
> 
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
> 

Hi Larry

Sure, that example __clone() isn’t exactly what I’d call short-and-sweet, but 
it absolutely solves the problem of: “what happens, in which order, when you do 
`clone ($foo) with (bar: ‘baz’, moo: ‘foo’);` on an object with a __clone() 
method. There is zero ambiguity there because it’s expressed in php the 
developer can see/debug/trace.

Also, consider that for classes that have no nullable properties, the __clone 
method could be a lot simpler than your example shows, e.g.:

        function __clone(?string $bar = null, ?int $baz = null, ?ComplexObj 
$foo = null) {
                $this->bar = $bar ?? $this->bar;
                $this->baz = $baz ?? $this->baz;
                $this->foo = $foo ?? clone ($this->foo);
        }


If the property hooks RFC had already passed a vote, I’d suggest that some 
expansion of hooks could provide a cleaner solution here, but it hasn’t yet 
(and I understand it’s not desirable to tie the fate of one RFC to another so 
heavily) so here we are.



Cheers


Stephen 
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to