如何让我的 PHP IDE 理解依赖注入容器?

How do I make my PHP IDE understand Dependency Injection Containers?(如何让我的 PHP IDE 理解依赖注入容器?)
本文介绍了如何让我的 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.

我查看了 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 理解依赖注入容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)