Skip to content

Partial Function Application for instance of non-static methods ("$this")#27

Open
arnaud-lb wants to merge 39 commits intopartials-v2-genfrom
pfa-this
Open

Partial Function Application for instance of non-static methods ("$this")#27
arnaud-lb wants to merge 39 commits intopartials-v2-genfrom
pfa-this

Conversation

@arnaud-lb
Copy link
Owner

@arnaud-lb arnaud-lb commented Jan 21, 2026

@arnaud-lb arnaud-lb force-pushed the pfa-this branch 4 times, most recently from fff0684 to 81530d0 Compare January 21, 2026 19:39
Copy link

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also include:

<?php
class Foo {
    public function dump(...$args) {
        var_dump($args);
    }
}
$f = Foo::dump(this: ?, ...);
$f(new Foo(), ...['this' => 'foo']);

as a test to make it clear that this: and 'this' unpacking can be combined?


?>
--EXPECTF--
Fatal error: Invalid use of 'this' placeholder in %s on line %d

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible the error message could be clearer here:

The 'this' placeholder may only be used on methods

or similar. I'll check existing error messages for a proper suggestion if/when the RFC passes.


?>
--EXPECTF--
Fatal error: Invalid parameter name: this in %s on line %d

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly:

The 'this' parameter must be a placeholder

@bwoebi
Copy link

bwoebi commented Feb 1, 2026

./sapi/cli/php -r 'class A { function b() { var_dump($this); } } $cb = A::b(this: ?); $next = $cb(this: ?); $next(new A);'

Fatal error: Invalid use of 'this' placeholder in Command line code on line 1
Stack trace:
#0 {main}

this should probably not fail.

This comes from the rename of this to __this, which is ... odd. I know the RFC acknowledges this, but it's really odd.
I'm aware that this comes from the implementation, but does it have to be that way?

Would it be possible to introduce a fallback instead, whereby this becomes a normal named parameter, when the callee is not a reference to a non-static method? I.e. add a slow-path with some specialized logic in zend_partial_create which moves this around?

@arnaud-lb
Copy link
Owner Author

Yes it's a compromise to avoid adding special cases in the engine to support $this as parameter name or variable name.

What we could probably do, is to set the param name to this in arginfo, after compilation, so that a PFA compiled as fn($__this) => $__this->fn() would be seen as a fn($this) from the outside.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments