首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在IDE中输入上下文提示?

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

Stack Overflow用户
提问于 2017-10-24 00:53:37
回答 1查看 193关注 0票数 5

我使用Closure::call (http://php.net/manual/en/closure.call.php)在类上下文中调用外部闭包。

下面是一个简单的复制品:

代码语言:javascript
复制
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)闭包将在另一个类中使用,并且它应该假定它的上下文?

EN

回答 1

Stack Overflow用户

发布于 2017-10-24 02:01:38

试一试

代码语言:javascript
复制
/** @var $this Foo */
print $this->bar;

它会将类Foo的自动补全添加到闭包中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46894614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档