Hello all,

I've created a feature request issue on GitHub (here: 
https://github.com/php/php-src/issues/10414), but I have been advised that it's 
best to post here.

What I would like to introduce/suggest, is the ability to create a closure from 
a method using the first-class-callable syntax (eg: MyClass::aMethod(...)), for 
a non-static method, statically. 

Currently, the following code causes an error.

```
class Test {
    public function test(): string { return 'test'; }
}
 
$closure = Test::test(...);
```

I understand why the error is thrown, but, and I'm unsure of the specifics 
regarding this, I think we could delay the error until the closure was called. 
The reason for this, is that closures can be bound, so if you followed on from 
the code above, you could do the following:

```
$closure->bindTo(new Test);
$closure();
```

The above would bind the closure in $closure to the scope of an object, which 
in this case, is the class that the method belongs to.

The best example I can think, for this, would be when filter a collection of 
instances. If you were using a collection library, you would currently have 
something like the following:

```
$collection->filter(function (Str $string) {
    return !$string->empty();
});
```

Whereas it would be much nicer to have the following:

```
$collection->filter(Str::empty(...));
```

In this situation, the collection library would be responsible for binding the 
closure to the value it is iterating.

I have limited experience with PHPs source, and C in general, but my 
understanding would be that if we were creating a closure, we would skip the 
check for the static method. The code responsible for handling the closure call 
would most require some additional functionality to check if it was bound to a 
valid instance, returning an error if it isn't, and then returning an error if 
it isn't bound at all and the method isn't static.

The more I think about it, the more I think this may require a new type of 
Closure, or at least a runtime applied interface, to help developers determine 
whether a closure was created using first-class-callable syntax.

I'm interested in feedback on this, and if anyone could point me towards the 
correct part of the codebase that handles this, I can investigate further. I am 
happy to create an RFC if others think this viable.

---
Best Regards,
*Ollie Read*

Reply via email to