我使用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"这就像预期的那样工作,但是我的集成开发环境当然不高兴,并且警告我“bar not found”字段:

我是否可以以某种方式告诉集成开发环境(在本例中为PhpStorm)闭包将在另一个类中使用,并且它应该假定它的上下文?
发布于 2017-10-24 02:01:38
试一试
/** @var $this Foo */
print $this->bar;它会将类Foo的自动补全添加到闭包中
https://stackoverflow.com/questions/46894614
复制相似问题