On Wed, May 21, 2025 at 09:13 Tim Düsterhus wrote:

> Am 2025-05-19 12:48, schrieb Volker Dusch:
>> We're still looking for feedback on the ...variadic approach to the
>> Syntax:
>> https://wiki.php.net/rfc/clone_with_v2#open_issues, as we only got one
>> reply so far on the topic.
> 
> ...
>
> *Some* property name being completely incompatible with “clone with” (no
> matter how the first parameter is going to be called) is a limitation
> that should not exist, it feels like a feature that is broken by design
> and I think I really would hate it if the documentation of this RFC
> would need a “Caution: It is not possible to reassign a property called
> '$object', due to a parameter name conflict”.
> 
> Adjusting the signature to `clone(object $object, array $withProperties)`
> would not have this problem and I don't consider the additional verbosity
> of an array literal to be a problem. Static analysis tools already
> understand array shapes and would need adjustments either way to
> understand the semantics.
> 
> From an implementation PoV a regular array parameter would also be
> simpler, since the implementation would be able to simply pass along the
> input array, whereas the “variadic” syntax needs special handling to
> combine positional parameters with named parameters into a single array
> that is then used in the cloning process.

The more I've thought about it, the more I also don't like the variadic
approach. It seems like a hack to try to get `property: value` syntax
for "free", but at a fundamental level the API doesn't make sense for
its purpose.

Not only does it prevent setting a property with the name of the first
parameter (with no workaround), but it also means that someone could
call the function with positional rather than named arguments, which
as the RFC admits "for clone this is usually not useful".

Even if it's slightly less ergonomic to quote property names in array keys,
I agree with Tim that it would be better to change the function signature to:

    function clone(object $object, array $withProperties): object {}

This simply removes the issue of not being able to set a certain property
name, as well as the confusing positional parameter behavior.

> Syntax-wise there might also be a middle-ground. Similarly to how this
> RFC turns `clone()` into a function, the `array()` syntax also looks
> like a function call and would naturally extend to named parameters.
> While mixing positional and named parameters would probably get complex,
> allowing purely named parameters would trivially be possible (without
> any function call overhead). It would also allow using the
> first-class-callable syntax with `array(...)`, something I would liked
> to have in the past. A proof of concept PR is at:
> 
> https://github.com/php/php-src/pull/18613
> 
> Combining named-parameter `array()` syntax with clone taking a array as
> the second parameter would allow for the following, which might combine
> the best of both worlds?
> 
>     clone($obj, array(foo: 1, bar: "baz", object: "this is not blocked"));

I really like this idea! If it can work without any function call overhead,
it would enable more ergonomic array creation not only for `clone()` but
also in many other common scenarios.

Regards,  
Theodore

Reply via email to