我在用Closure::call
(http://php.net/manual/en/closure.call.php 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"
This works as expected, but my IDE is of course, not happy, and is warning me about the "field bar
not found":
我能否以某种方式告诉 IDE(在本例中为 PhpStorm)该闭包将在另一个类中使用并且它应该假定其上下文?
try
/** @var $this Foo */
print $this->bar;
除了 Closure 之外,它还会为 Foo 类添加自动完成功能
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)