Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Matthias Noback:
Decoupling from a service locator
Nov 12, 2014 @ 09:58:06

In his latest post Matthias Noback shares a few hints on how yuo can decouple from using a service locator in your application. A service locator (much like a dependency injection container) is a centralized place for storing and creating instances of objects in your apps with a bit more structure than just random "new" calls.

"Decoupling from a service locator - shouldn't that be: don't use a service locator?" Well, not really, since there are lots of valid use cases for using a service locator. The main use case is for making things lazy-loading (yes, you can also use some kind of proxy mechanism for that, but let's assume you need something simpler).

He starts with an example dispatcher class and shows how to modify the flow so that "expensive" listeners are only created in the correct context. He also suggests a few other methods for handling the idea of dependency inversion a service locator provides: using closures/callables instead of classes and using something called a "synthetic service", one set up at runtime as synthetic and used as needed on a manual basis (like in his bundle example).

tagged: decouple servicelocator dependency closure synthetic class tutorial

Link: http://php-and-symfony.matthiasnoback.nl/2014/11/decoupling-from-a-service-locator/

Trending Topics: