website/errors/argument.staticClosure.md
<?php declare(strict_types = 1);
class Foo
{
/**
* @param-closure-this Foo $callback
*/
public function doFoo(Closure $callback): void
{
}
}
$foo = new Foo();
$foo->doFoo(static function (): void {
// ...
});
The parameter expects a bindable closure (one that can access $this), but a static closure was passed. Static closures declared with the static keyword cannot be bound to an object, so they cannot access $this.
A method advertises that its parameter needs a bindable closure by using the @param-closure-this PHPDoc tag. Passing a static closure to such a parameter will fail at runtime.
Remove the static keyword from the closure:
-$foo->doFoo(static function (): void {
+$foo->doFoo(function (): void {
// ...
});