Re: [RFC] Partial Function Application for instance of non-static methods ("$this")

From: Date: Fri, 06 Feb 2026 16:43:29 +0000
Subject: Re: [RFC] Partial Function Application for instance of non-static methods ("$this")
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-130033@lists.php.net to get a copy of this message
Hi On 1/22/26 17:41, Larry Garfield wrote:
More spitballing on my previous reply: class Test { public function stuff(int $a) {} } (Test)?->stuff(?);
As mentioned in the sibling mail, this is existing syntax and thus doesn't work.
But if there's no partialing on the method side, it could get abbreviated to: (?)->stuff(4); Translates to: fn(object $__this) => $__this->stuff(4); Because we don't need to know the type at compile time, and a method-not-found error would still happen at runtime anyway if necessary.
By that argument you wouldn't need to type the argument as object either (calling methods on a non-object will throw) and the comparison would become:
    $c = (?)->stuff(4);
    $c = fn($o) => $o->stuff(4);
Keeping full type information is the main benefit of PFA over “just write a Closure”. Being able to reorder parameters as part of partial application is another explicit feature that would not be supported by that syntax.
That would then be a lot easier to write in cases where you're just dropping a $this->stuff() call into a pipe chain but want to receive $this.
The use case for partially applying $this is in cases where you need a “function handle”, that's why the examples are ones where the resulting Closure is passed as a parameter to another function. Within a pipe chain you would just use the regular -> operator on the result of the previous step:
    $result = (trim($username)
        |> $repository->findBy(name: ?)
        )->getId();
It would also naturally support ?-> in case your repository returns null when the user cannot be found. Best regards Tim Düsterhus

« previous php.internals (#130033) next »