Hi

Am 2026-01-22 16:33, schrieb Derick Rethans:
https://wiki.php.net/rfc/partial_function_application_this

        We propose allowing a named parameter $this: ? to be combined with the
        “scope resolution operator” syntax of referring to a method.

I can't say I am entirely sold on the syntax here — starting with a $
makes it look like a variable.

Yes, it certainly requires some getting used to it. It's the best we could come up with, but if there are any better suggestions, we're open to hearing those. I'd like to note the “Rejected Features” section, since it lists some non-workable alternatives we considered.

        Since the $this: ? parameter does not refer to a parameter in
        the underlying argument list and thus there is no ambiguity with
        regard to positional parameters it may be placed at any position
        of the parameter list

I think I would be keen on *requiring* this to be the **first** in the
argument list.

Although there is no ambiguity, I also can't see why you would want to
have it anywhere else.

Being able to “reorder” parameters to (better) fit a specific callback signature is an explicit feature of PFA that we also wanted to support for `$this`.

As an example, suppose objects could be used as an array key. `array_find()` first passes the value and then the key to the callback. In this case users might want to do the following:

    class SomeObject {
        public function __construct(private string $value) { }
public function hasMatchingValue(string $value) { return $value === $this->value; }
    }

    $array = [
        new SomeObject('foo') => 'bar',
        new SomeObject('baz') => 'baz',
    ];

array_find(SomeObject::hasMatchingValue(value: ?, $this: ?), $array);

In this case `array_find()` is supposed to return `'baz'`, since this is where the values match.

        It may also not be used when partially applying an instance
        method with a known object.

$c = $dateTime->format($this: ?, "c"); // Fatal error: Invalid use of $this placeholder

Can you explain (in the RFC) what you mean by "known object" — I don't
think the language on what this means is clear enough.

I have rephrased that to “It may also not be used when partially applying an instance method using the -> operator, since the object is already specified:”

Best regards
Tim Düsterhus

Reply via email to