我正在使用 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
”:
我能否以某种方式告诉 IDE(在本例中为 PhpStorm)闭包将在另一个类中使用并且它应该采用其上下文?
最佳答案
尝试
/** @var $this Foo */
print $this->bar;
它会在 Closure 之外为类 Foo 添加自动完成
关于php - 如何在 IDE 中键入提示上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894614/