I use Closure::call
( http://php.net/manual/en/closure.call.php ) to trigger an external closure inside the class context.
Here is simple:
class Foo {
private $bar = 'baz';
public function callClosureInThisContext(\Closure $closure) {
return $closure->call($this);
}
}
class Closures {
public function getClosureForFoo() : \Closure {
return function () {
print $this->bar;
};
}
}
$foo = new Foo();
$closures = new Closures();
$foo->callClosureInThisContext($closures->getClosureForFoo());
This works as expected, but my IDE, of course, is not happy and warns me about the "field bar
not found":

Is there any way to tell the IDE (in this case PhpStorm) that the closure will be used inside another class and that it should accept its context?
source
share