Search code examples
phpdesign-patternsdependency-injectioninversion-of-controlservice-locator

Service Locator, Dependency Injection (and Container) and Inversion of Control


I've been programming for some time but never got interested in knowing in theory what each concept means, I may be using a variety of programming concepts, but without knowing it.

Service Locator: For me, refers to a record of shortcuts to speed up development by reducing the amount of code. One question is: may Locator refer to namespaces/classes only, or I can have a registry of variables?

Here is my understanding of it:

$locator = new ServiceLocator()
$locator->set('app', new System\Application());
$locator->set('db', new System\Pdo());

// Get the objects
$locator->get('db')->connect();
$locator->get('app')->run();

Dependency Injection (and Dependency Injection Container): Injecting objects within objects, allowing faster access to these regardless of the factory pattern. And DI Container?

Here is my understanding of it:

$app = new System\Application(System\Config::load());

Inversion of Control: Don't understand this Design Pattern (or understand but don't know if what I do is IoC)

Then, in theory (preferably with simple examples), what does each of these concepts mean? Am I correct, or what is wrong / can be improved?

Thanks!


Solution

  • I think you understood correctly the Service Locator.

    About Dependency Injection, means that if an object has constructor and/or property dependencies, these are injected in the object by outside, as opposed to the object getting the dependencies by itself

    public class MyClass
    {
       private $_dep;
       public function __construct($dep=null)
       {
           //$dep has to be injected
           $this->_dep=$dep;                           
       }
    
       //this is wrong because it couples MyClass to a specific Dependency implementation
       public function __construct()
       {
           $this->_dep=new Dependency();
        }
    }
       $dep=new Dependency();
       $obj=new MyClass($dep);
    

    Usually the constructor takes an abstraction (an interface) as the param, and outside the class is instantiated a concrete implementation which then is passed on to the constructor when creating a new instance of MyClass.

    A DI Container, handles the dependency injection automatically. You just configure it, so that it knows what concrete classes to be returned when an abstraction is asked. The Container handles object creation, injecting dependencies via constructor and/or properties. Depending on the container (I don't know an example for php, I'm familiar only with .net DI Containers) you might have to register also the types of objects that can be created by it.

    Inversion of Control means that instead of a higher level class depending on a lower level class (dependency) implementation, the control is inversed so the lower level class implementation depends on an abstraction required by the higher level class.

    //abstraction defined for the use of higher level class
    public interface  IRepository {}
    
    // that's the dependency, the lower level class  
    public class XmlRepository implements IRepository {}
    
    //the higher level class
     public class MyClass
     {
         public function __construct(IRepository $repo) {}
      }
    

    IoC and DiC go together as the DI Container offers the IoC feature.