如何在IDE中输入提示上下文?

时间:2017-10-23 16:53:37

标签: php intellij-idea ide phpstorm phpdoc

我正在使用Closure::callhttp://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字段”: enter image description here

我可以以某种方式告诉IDE(在本例中为PhpStorm)闭包将在另一个类中使用并且它应该假设它的上下文吗?

2 个答案:

答案 0 :(得分:0)

/** @var $this Foo */
print $this->bar;

除了Closure之外,它还会为类Foo添加自动完成功能

答案 1 :(得分:0)

我不认为可以用phpdoc完成。

查看reference of phpDocumentor,类似这样的最近标记是@uses and @used-by tag

但我在您的示例代码上尝试了这些,但它并没有做您想做的事情。

相关问题