Re: [RFC] Partial Function Application for instance of non-static methods ("$this")
| From: | Tim Düsterhus | Date: | Thu, 22 Jan 2026 16:08:21 +0000 |
| Subject: | Re: [RFC] Partial Function Application for instance of non-static methods ("$this") | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-129859@lists.php.net to get a copy of this message | ||
Hi
Am 2026-01-22 16:33, schrieb Derick Rethans:
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.https://wiki.php.net/rfc/partial_function_application_thisWe 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.
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