如何使我的PHP IDE了解依赖注入容器? [英] How do I make my PHP IDE understand Dependency Injection Containers?

查看:78
本文介绍了如何使我的PHP IDE了解依赖注入容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前情况:我通过使用依赖注入来解决我的项目中的依赖关系。我想采用下一个逻辑步骤,使用依赖注入容器(DIC)来简化对依赖关系的管理和延迟加载类。

Current situation: I have dependencies in my project that I solve by using dependency injection. I want to take the next logic step by using a dependency injection container (DIC) to ease the management of my dependencies and to lazy-load classes.

a href =https://github.com/troelskn/bucket =noreferrer> Bucket , Pimple sfServiceContainer 进行了一些测试,真的很感激DIC的工作。由于它的简单性和原始的力量,我可能会去寻找疙瘩。如果我没有这个问题:

I looked at Bucket, Pimple, and sfServiceContainer, ran some test and really appreciate how DIC’s work. I’d probably go for Pimple because of its simplicity and raw power. If I didn’t have this problem:

由于DIC提供的抽象,我使用的IDE(PHPStorm)不再了解我的代码中发生了什么。它不明白$ container ['mailer']或$ sc-> mailer正在持有一个类对象。我也尝试过Netbeans IDE:同样的问题。

Due to the abstraction that DIC’s offer, the IDE I’m using (PHPStorm) no longer understands what’s going on in my code. It doesn’t understand that $container['mailer'] or $sc->mailer is holding a class object. I also tried Netbeans IDE: same problem.

这对我来说真的是一个问题,因为我的IDE变得无用了。在处理类时,我不想编程没有代码提示,自动完成和重构工具。我不希望我的IDE在验证代码时找到各种误报。

This is really a problem for me because my IDE becomes useless. I don’t want to program without code hints, autocompletion and refactoring tools when dealing with classes. And I don’t want my IDE to find all kinds of false positives when validating code.

所以我的问题是:有没有人处理这个问题,找到一个解决方案?

So my question is: Has anyone dealt with this problem and found a solution?

推荐答案

您可以定义变量手动的类:

You can define class of the variable 'manually':

/** @var YourClassType $mailer */
$mailer = $container['mailer'];

在PhpStorm中(和标准),使用两个星号,并在变量的名称之前写入数据类型。

In PhpStorm (and by standards), use two asterisks and write the data type before the name of the variable.

您可以编写没有变量名称的数据类型(而不是没有数据类型的名称)。

You can write the data type without the name of the variable (but not the name without the data type).

这篇关于如何使我的PHP IDE了解依赖注入容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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