通过Symfony2依赖注入保留自动完成功能 [英] Preserving auto-completion abilities with Symfony2 Dependency Injection

查看:73
本文介绍了通过Symfony2依赖注入保留自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP Storm作为我的IDE,但是我相信其他IDE(例如Netbeans)将遇到与我将在下面解释的相同的问题.

I'm using PHP Storm as my IDE, but I believe that other IDE's such as Netbeans will have the same issue as I'll explain below.

使用Symfony2之类的框架时,我们添加了精彩的Dependency Injection世界.因此,可以使用下面的代码片段简单地实例化对象:

When using a framework like Symfony2, we have the wonderful world of Dependency Injection added. So objects can simply be instantiated using code like the following snippet:

$myThingy = $this->get('some_cool_service');

这非常方便,因为对象已经预先配置.一个问题是,由于IDE不知道get()方法返回的是哪种类型,因此基本上所有的PHP IDE都将自动完成完全中断.

This is very handy, as objects are already configured beforehand. The one problem is, that auto-completion breaks entirely in basically any PHP IDE, as the IDE does not know what type the get() method is returning.

有没有办法保留自动完成功能?例如,创建Controller的扩展将是答案吗?例如:

Is there a way to preserve auto-completion? Would creating for example an extension of Controller be the answer? For example:

class MyController extends Controller {
    /**
     * @return \MyNamespace\CoolService
     */
    public getSomeCoolService() {
        return new CoolService();
    }
}

然后对于应用程序控制器,将MyController指定为基类而不是Controller?

and then for application controllers, specify MyController as the base class instead of Controller?

使用Factory类或任何其他可能的方法怎么办?

What about using a Factory class, or any other possible methods?

推荐答案

它涉及更多,但是您仍然可以使用Eclipse PDT来做到这一点:

It is more involving, but you can still do this with eclipse PDT:

$myThingy = $this->get('some_cool_service');
/* @var $myThingy \MyNamespace\CoolService */

更新: 此页面上的示例显示,您还可以对phpStorm使用另一种方法:

UPDATE: The example on this page shows you may also use the other way round with phpStorm:

$myThingy = $this->get('some_cool_service');
/* @var \MyNamespace\CoolService $myThingy */

这篇关于通过Symfony2依赖注入保留自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆