php - 如何在 IDE 中键入提示上下文?

标签 php intellij-idea ide phpstorm phpdoc

我正在使用 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”: enter image description here

我能否以某种方式告诉 IDE(在本例中为 PhpStorm)闭包将在另一个类中使用并且它应该采用其上下文?

最佳答案

尝试

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

它会在 Closure 之外为类 Foo 添加自动完成

关于php - 如何在 IDE 中键入提示上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894614/

相关文章:

php - 哪些工具可以在 PHP 中构建 SOAP 服务器?

php - 如何通过从另一个下拉列表中选择值来填充下拉列表?

node.js - 使用 Karma 覆盖 NodeJS

plugins - 如何安装 IntelliJ 插件而不将其上传到 IntelliJ

javascript - 通过 ajax 在模态中从 <select> 传递值时发生内部服务器错误

php - 如何从列略有不同的两个表中选择行?我怎样才能抵消它们?

java - 使用 Apache Maven 在 IntelliJ 中构建 Java 项目的内存问题,发生在 Java 7 中,使用 Java 8 构建良好

xcode - 使用 Xcode 作为 Javascript IDE,可以吗?

intellij-idea - Intellij IDEA 社区与教育

multithreading - 在Visual Studio 2010中是否可以中断除一个线程以外的所有线程?