Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Tests Merge branch 'master' into feature/class-instead-of-namespace Mar 9, 2017
uml Added More pattern UMLs Jul 31, 2014
LogService.php PHP7 ServiceLocator Sep 22, 2016
README.rst Improve grammar: these code > this code Apr 4, 2017
ServiceLocator.php PHP7 ServiceLocator Sep 22, 2016

README.rst

Service Locator

THIS IS CONSIDERED TO BE AN ANTI-PATTERN!

Service Locator is considered for some people an anti-pattern. It violates the Dependency Inversion principle. Service Locator hides class' dependencies instead of exposing them as you would do using the Dependency Injection. In case of changes of those dependencies you risk to break the functionality of classes which are using them, making your system difficult to maintain.

Purpose

To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code. DI pattern and Service Locator pattern are an implementation of the Inverse of Control pattern.

Usage

With ServiceLocator you can register a service for a given interface. By using the interface you can retrieve the service and use it in the classes of the application without knowing its implementation. You can configure and inject the Service Locator object on bootstrap.

Examples

  • Zend Framework 2 uses Service Locator to create and share services used in the framework(i.e. EventManager, ModuleManager, all custom user services provided by modules, etc...)

UML Diagram

Alt ServiceLocator UML Diagram

Code

You can also find this code on GitHub

ServiceLocator.php

.. literalinclude:: ServiceLocator.php
   :language: php
   :linenos:

LogService.php

.. literalinclude:: LogService.php
   :language: php
   :linenos:

Test

Tests/ServiceLocatorTest.php

.. literalinclude:: Tests/ServiceLocatorTest.php
   :language: php
   :linenos: