我有一些服务,在其他服务中也有一些依赖关系,因此,我决定@service_container
在每个服务中注入,而不是非常具体的依赖关系。结果是这样的:
class InternalComponentHelper implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function somefunction(){
//Do something with the container
}
}
然后在我的服务定义中
intcomponent_helper:
class: AplicacionBaseBundle\DependencyInjection\Helpers\InternalComponentHelper
calls:
- [setContainer, ["@service_container"]]
我需要知道的是这是否是不好的做法,以及它可能导致什么危害或性能问题。顺便说一下,没有循环依赖。
完全注入服务容器被认为是不好的做法。根据此博客文章:
尽管很容易做到,但直接使用容器并不是一个好习惯,因为它隐藏了类的依赖关系,使它们与外部配置耦合在一起,因此更难测试,更难检查等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句