使用 Symfony2 依赖注入保留自动完成功能
我使用 PHP Storm 作为 IDE,但我相信其他 IDE(例如 Netbeans)也会遇到相同的问题,我将在下面解释。
当使用像 Symfony2 这样的框架时,我们添加了依赖注入的美妙世界。因此,可以使用如下代码片段简单地实例化对象:
$myThingy = $this->get('some_cool_service');
这非常方便,因为对象已经预先配置。一个问题是,自动完成功能在基本上任何 PHP IDE 中都会完全中断,因为 IDE 不知道 get() 方法返回什么类型。
有没有办法保留自动完成功能?例如,创建控制器的扩展是答案吗?例如:
class MyController extends Controller {
/**
* @return \MyNamespace\CoolService
*/
public getSomeCoolService() {
return new CoolService();
}
}
然后对于应用程序控制器,指定MyController作为基类而不是Controller?
使用工厂类或任何其他可能的方法怎么样?
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.
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');
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.
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();
}
}
and then for application controllers, specify MyController as the base class instead of Controller?
What about using a Factory class, or any other possible methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它更复杂,但您仍然可以使用 eclipse PDT 来完成此操作:
更新:
此页面上的示例显示您也可以使用 phpStorm 的相反方式:
It is more involving, but you can still do this with eclipse PDT:
UPDATE:
The example on this page shows you may also use the other way round with phpStorm:
您可以在控制器中定义私有属性
You could define private properties in your controllers
我使用基本控制器类来进行捆绑。您需要在方法中注释 return。至少在 Eclipse 上是有效的。
我不喜欢 /*var ... */,因为它在代码中包含了太多内容。
我不喜欢私有属性,因为您可能会错误地认为服务已经加载。
I use base Controller class for bundle. You need to annotate the return in method. At least that works on Eclipse.
I don't like /*var ... */, because it gets too much into code.
I don't like private properties, because you can wrongly assume that services are already loaded.
我使用 Komodo Studio,并使用 @var 标记变量,即使在方法内部,也可以为我保留自动完成功能。
I use Komodo Studio, and tagging variables with @var, even inside methods, preserves auto completion for me.
使用 netbeans IDE 7.1.2 PHP
working with netbeans IDE 7.1.2 PHP