In Symfony2, why is it a bad idea to inject the service container, rather than individual services?

后端 未结 4 812
暖寄归人
暖寄归人 2020-11-30 02:31

I can\'t find the answer to this...

If I inject the service container, like:

// config.yml
my_listener:
   class: MyListener
   arguments: [@service_         


        
4条回答
  •  执笔经年
    2020-11-30 03:04

    Injecting the whole container is not a good idea in general. Well, it works, but why injecting the whole container, while you only need a few other services or parameters.

    Sometimes you want to inject the whole container to avoid circular references, because if you inject the whole container, you get "lazy loading" of the services you require. An example would be doctrine entity listeners.

    You can get the container from every class that is "Container Aware" or has access to the kernel.

提交回复
热议问题