Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What are bad sides of using Symfony's ServiceLocator?

The college told me that sometimes I overuse ServiceLocator in Symfony 5.4 project.
I never considered that possibility. Everything seems straightforward => you get services from the container as you usually do. Instead of using a factory, you use a service locator and you do not care about dependencies.

So, am I right here, or it is possible to overuse ServiceLocator in Symfony?
If yes please advice when it is ok to use it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should use inversion of control through dependency injection.
Main advantages of this approach (in no specific order and maybe I’m forgetting some of them)

  • you’re not tied to the whole container
  • you have a clear overview of all dependency
  • you can write code over interfaces instead of concrete implementations
  • your classes become more testable

So if you’re using service locator everywhere for your dependencies, I agree that you’re overusing it

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading