作者:小蚊子 | 来源:互联网 | 2023-01-11 19:37
我正在使用Closure::call
(http://php.net/manual/en/closure.call.php)在类上下文中调用外部闭包.
这是一个简单的复制品:
class Foo {
private $bar = 'baz';
/**
* Executes a closure in $this context and returns whatever the closure returns.
*
* @param \Closure $closure
* @return mixed
*/
public function callClosureInThisContext(\Closure $closure) {
return $closure->call($this);
}
}
class Closures {
/**
* @return \Closure
*/
public function getClosureForFoo() : \Closure {
return function () {
// how do I tell my IDE that in this context $this is actually class Foo,
// and not the class Closures?
print $this->bar;
};
}
}
$foo = new Foo();
$closures = new Closures();
$foo->callClosureInThisContext($closures->getClosureForFoo()); // prints "baz"
这按预期工作,但我的IDE当然不高兴,并警告我" bar
找不到字段":
![在此输入图像描述](https://img.php1.cn/3cd4a/1eebe/cd5/c72d78d7317a9e8e.webp)
我可以以某种方式告诉IDE(在本例中为PhpStorm),闭包将在另一个类中使用,并且它应该假设它的上下文?