Re: Re: [RFC] Clone with v2

From: Date: Thu, 22 May 2025 12:13:06 +0000
Subject: Re: Re: [RFC] Clone with v2
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Wed, May 21, 2025 at 23:27 Larry Garfield wrote:
> On Wed, May 21, 2025, at 9:13 AM, 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”.
> 
> I completely disagree here.  The __ prefix is sufficient to solve the issue.
> A double underscore prefix has been understood to mean "internal to PHP, magic
> here" for at least 20 years.  The chances of someone having a property called
> $__object are virtually nil, and if they do, they're already treading on naming
> that's reserved for PHP's use anyway so if it breaks, it's their own fault.

Is the __ prefix part of the current proposal? Currently the RFC examples have
the parameter name as simply $object. Even if the parameter is renamed to
$__object, there would still have to be a warning in the docs that it isn't
possible to use the function to set a property named __object, which I agree
with Tim is a limitation that should not exist.

For people who want to consistently use named arguments for all parameters,
(rather than all parameters except the first one) the code would look like:

    $obj = clone(
        __object: $this,
        foo: 'bar',
        object: $baz,
    );

Is this API really better than with a simple array parameter?

    $obj = clone($this, [
        'foo' => 'bar',
        'object' => $baz,
    ]);

Using an array results in fewer lines of code, and the distinction between
the object being cloned and the properties being set is much clearer.

I get that it may be annoying to quote array keys, but to me this is not
as bad as the confusing variadic API. In the future PHP could get a simpler
array syntax like [foo: 'bar'], which would benefit far more functions than
only clone().

Kind regards,
Theodore


Thread (43 messages)

« previous php.internals (#127426) next »