简体   繁体   English

PHP-DI 使用 set 方法时潜在的多态调用

[英]PHP-DI Potentially polymorphic call when using the set method

The issue问题

I have an unexpected warning from PHPStorm when I try to set a new value in a PHP-DI container.当我尝试在 PHP-DI 容器中设置新值时,我收到来自 PHPStorm 的意外警告。

Given the following code:鉴于以下代码:

function inject(Psr\Container\ContainerInterface $container){
    $container->set(RandomClass::class, new RandomClass());
}

$container = new DI\Container(); class is instantiated

inject($container);

The following warning is triggered触发了以下警告

Potentially polymorphic call.潜在的多态调用。 does not have members in its hierarchy在其层次结构中没有成员

I understand what the warning means, but I do not see why it pops up, especially since I have not found any occurrences of this situation while looking on Google and SO and the documentation does not mention it.我理解警告的含义,但我不明白为什么会弹出它,特别是因为我在查看 Google 和 SO 时没有发现任何这种情况,并且文档没有提到它。

Is there something I am missing, or is this a "false positive" ?有什么我遗漏的,还是“误报”?

The reason behind the issue问题背后的原因

Given the following code (which is very similar to the one I use)鉴于以下代码(与我使用的代码非常相似)

function inject(Psr\Container\ContainerInterface $container){
    $container->set(RandomClass::class, new RandomClass());
}

$container = new DI\Container(); class is instantiated

inject($container);

The $container->set(...) call is going to trigger the following warning $container->set(...)调用将触发以下警告

Potentially polymorphic call.潜在的多态调用。 does not have members in its hierarchy在其层次结构中没有成员

This is to be expected as Psr\\Container\\ContainerInterface only contains definitions for the following methods这是意料之中的,因为Psr\\Container\\ContainerInterface仅包含以下方法的定义

  • get($id)
  • has($id)

The solution解决方案

Two possible solutions for this issue:此问题的两种可能解决方案:

  • Type the methods directly with the container, making sure to not use the FQN of the class but only use Container and "use the namespace", it will make changing to a new container package easier (because this is still the goal behind PSRs, being able to almost hot-swap packages).直接在容器中输入方法,确保不使用类的 FQN 而只使用Container和“使用命名空间”,这将使更改为新的容器包更容易(因为这仍然是 PSR 背后的目标,被几乎可以热插拔封装)。
  • Create a custom interface based on Psr\\Container\\ContainerInterface and add the required methods to it.创建基于Psr\\Container\\ContainerInterface的自定义接口, Psr\\Container\\ContainerInterface添加所需的方法。

Or, eventually, you can try to make PHP-FIG extend the PSR-11 standard to include a standard set($id, $value) method.或者,最终,您可以尝试使PHP-FIG扩展PSR-11标准以包含标准set($id, $value)方法。

The set() method is not part of Psr\\Container\\ContainerInterface . set()方法不是Psr\\Container\\ContainerInterface

If you want to use that method, you can't typehint against the interface because your code explicitly needs a PHP-DI instance.如果您想使用该方法,则不能针对接口键入提示,因为您的代码明确需要一个 PHP-DI 实例。

Your code doesn't have to be generic, don't overthink things too much.你的代码不必是通用的,不要想太多。 The PSR is useful mostly for frameworks and libraries (who need to be compatible with multiple containers), not for end-users. PSR 主要用于框架和库(需要与多个容器兼容),而不是最终用户。

The day you switch container library you will have many more complex things to do than just replacing the set() call.在您切换容器库的那一天,您将有更多复杂的事情要做,而不仅仅是替换set()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM