I can't find the answer to this...
If I inject the service container, like:
// config.yml my_listener: class: MyListener arguments: [@service_container] my_service: class: MyService // MyListener.php class MyListener { protected $container; public function __construct(ContainerInterface $container) { $this->container = $container; } public function myFunction() { $my_service = $this->container->get('my_service'); $my_service->doSomething(); } }
then it works just as well as if I do:
// config.yml my_listener: class: MyListener arguments: [@my_service] my_service: class: MyService // MyListener.php class MyListener { protected $my_service; public function __construct(MyService $my_service) { $this->my_service = $my_service; } public function myFunction() { $this->my_service->doSomething(); } }
So why shouldn't I just inject the service container, and get the services from that inside my class?
My list of reasons why you should prefer injecting services:
Your class is dependent only on the services it needs, not the service container. This means the service can be used in an environment which is not using the Symfony service container. For example, you can turn your service into a library that can be used in Laravel, Phalcon, etc - your class has no idea how the dependencies are being injected.
By defining dependencies at the configuration level, you can use the configuration dumper to know which services are using which other services. For example, by injecting @mailer
, then it's quite easy to work out from the service container where the mailer has been injected. On the other hand, if you do $container->get('mailer')
, then pretty much the only way to find out where the mailer is being used is to do a find
.
You'll be notified about missing dependencies when the container is compiled, instead of at runtime. For example, imagine you have defined a service, which you are injecting into a listener. A few months later, you accidentally delete the service configuration. If you are injecting the service, you'll be notified as soon as you clear the cache. If you inject the service container, you'll only discover the error when the listener fails because of the container cannot get the service. Sure, you could pick this up if you have thorough integration testing, but ... you have got thorough integration testing, haven't you? ;)
You'll know sooner if you are injecting the wrong service. For example, if you have:
public function __construct(MyService $my_service) { $this->my_service = $my_service; }
But you've defined the listener as:
my_listener: class: Whatever arguments: [@my_other_service]
When the listener receives MyOtherService
, then PHP will throw an error, telling you that it's receiving the wrong class. If you're doing $container->get('my_service')
you are assuming that the container is returning the right class, and it can take a long time to figure out that its' not.
If you're using an IDE, then type hinting adds a whole load of extra help. If you're using $service = $container->get('service');
then your IDE has no idea what $service
is. If you inject with
public function __construct(MyService $my_service) { $this->my_service = $my_service; }
then your IDE knows that $this->my_service
is an instance of MyService
, and can offer help with method names, parameters, documentation, etc.
Your code is easier to read. All your dependencies are defined right there at the top of the class. If they are scattered throughout the class with $container->get('service')
then it can be a lot harder to figure out.
Your code is easier to unit test. If you're injecting the service container, you've got to mock the service container, and configure the mock to return mocks of the relevant services. By injecting the services directly, you just mock the services and inject them - you skip a whole layer of complication.
Don't be fooled by the "it allows lazy loading" fallacy. You can configure lazy loading at configuration level, just by marking the service as lazy: true
.
Personally, the only time injecting the service container was the best possible solution was when I was trying to inject the security context into a doctrine listener. This was throwing a circular reference exception, because the users were stored in the database. The result was that doctrine and the security context were dependent on each other at compile time. By injecting the service container, I was able to get round the circular dependency. However, this can be a code smell, and there are ways round it (for example, by using the event dispatcher), but I admit the added complication can outweigh the benefits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With