0

I have some services, which also have some dependencies in other services, so, I decided to inject the @service_container in each one of these services, instead of the very specific dependencies. The result is something like this:

class InternalComponentHelper implements ContainerAwareInterface
{
    use ContainerAwareTrait;

   public function somefunction(){
       //Do something with the container
   }
}

Then in my service definition

      intcomponent_helper:
        class: AplicacionBaseBundle\DependencyInjection\Helpers\InternalComponentHelper
        calls:
          - [setContainer, ["@service_container"]]

What I need to know is if this is a bad practice and what harm or performance issue could it cause, if any. There are no circular dependencies by the way.

  • 1
    [Probably.](https://xkcd.com/1691/) – SIGSTACKFAULT Apr 12 '18 at 18:17
  • That's a bad practice injecting container. @see more detailed answers [here](https://stackoverflow.com/questions/23931321/in-symfony2-why-is-it-a-bad-idea-to-inject-the-service-container-rather-than-i) – birkof Apr 13 '18 at 06:41

1 Answers1

1

It is considered bad practice to inject a service container at all. According to this blog post:

As easy as this seams to be, using the container directly is not considered a good practice because it hides the dependencies of your classes, making them coupled to external configuration, thus harder to test, harder to review, etc.

EmilCataranciuc
  • 1,024
  • 1
  • 11
  • 24