tl;dr of DI vs SL

Chris Hartjes has a nice writeup on dependency injection containers versus service locators. Here’s a short way to tell which one you’re using:

“If your class has a dependency on a container, you’re using Service Locator, not Dependency Injection.”

If you have a DI container and you pass it into a class so that class can get its own dependencies from that container, you are *still* doing Service Locator. It doesn’t matter that the locator is called a DI container. The key is that the object is pulling in its dependencies, instead of something outside of the object pushing the dependencies into it.

This entry was posted in PHP, Programming. Bookmark the permalink.