問題描述
當(dāng)前情況:我的項(xiàng)目中有依賴項(xiàng),我使用依賴項(xiàng)注入解決了這些依賴項(xiàng).我想采取下一個(gè)邏輯步驟,使用依賴項(xiàng)注入容器 (DIC) 來簡化依賴項(xiàng)的管理和延遲加載類.
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,運(yùn)行了一些測試并真正欣賞 DIC 的工作方式.我可能會選擇 Pimple,因?yàn)樗暮唵涡院驮剂α?如果我沒有這個(gè)問題:
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) 不再理解我的代碼中發(fā)生了什么.它不明白 $container['mailer'] 或 $sc->mailer 持有一個(gè)類對象.我也試過 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.
這對我來說真的是個(gè)問題,因?yàn)槲业?IDE 變得毫無用處.在處理類時(shí),我不想在沒有代碼提示、自動完成和重構(gòu)工具的情況下進(jìn)行編程.而且我不希望我的 IDE 在驗(yàn)證代碼時(shí)發(fā)現(xiàn)各種誤報(bào).
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.
所以我的問題是:有沒有人處理過這個(gè)問題并找到了解決方案?
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(以及標(biāo)準(zhǔn))中,使用兩個(gè)星號并在變量名稱之前寫入數(shù)據(jù)類型.
In PhpStorm (and by standards), use two asterisks and write the data type before the name of the variable.
可以寫出沒有變量名的數(shù)據(jù)類型(但不能寫出沒有數(shù)據(jù)類型的名字).
You can write the data type without the name of the variable (but not the name without the data type).
這篇關(guān)于如何讓我的 PHP IDE 理解依賴注入容器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!