当前情况:我的项目中有依赖项,我使用依赖项注入解决了这些依赖项.我想采取下一个逻辑步骤,使用依赖项注入容器 (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.
我查看了 Bucket、Pimple 和 sfServiceContainer,运行了一些测试并真正欣赏 DIC 的工作方式.我可能会选择 Pimple,因为它的简单性和原始力量.如果我没有这个问题:
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 理解依赖注入容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!