king23 dependency injector

1.1.0 2017-04-25 09:47 UTC


License Latest Stable Version Total Downloads Scrutinizer Code Quality Code Coverage SensioLabsInsight

King23/DI, a dependency injection lib for King23

Copyright (C) 2015 by Peter Petermann All rights reserved.


King23/DI is licensed under a MIT style license, see for further information


  • PHP 5.6 (might run on earlier versions, untested)
  • LINUX / MAC OS X (might run on windows, untested)



install using composer:

Hint: King23/DI follows Semantic Versioning v2.0.0, so you can set your version in composer to something like "~1.0"


Basically the container offers 3 Methods:

  • void register(string $interface, callable $implementation)
  • void registerFactory(string $interface, callable $implementation)
  • mixed getInstanceOf(string $classname)

register($interface, $implementation) is used to register dependencies for injection, while $interface can be any string in theory, it is meant to hold the name of an interface. The callable is supposed to be a method which should return a new instance of the interface (there is really nothing build in to stop you from using arbitrary strings, but the automated injection in the constructors when using getInstanceOf is using the type hints of the parameters. If you use any other string you can't have it automatic injected - which kind of voids the point. That said, it still can be of use when integrating King23/DI with frameworks that require specific keys in the DI). The object returned here will only be instanced once, further calls will return the same instance.

registerFactory($interface, $implementation) works the same way register() does, except that each time an instance of $interface is requested a new instance will be created.

getInstanceOf($classname), will return an object of type $classname, which gets the dependencies injected that are defined by the interfaces used in the type hints of its constructor. If called with a class/interfacename that has been used as a key in register/registerFactory, it will actually return an instance of the registered service.


    // get an Instance of the container
    $container = \King23\DI\DependencyContainer();
    // register a singleton service
    $container->register(\ExampleInterface::class, function() {
        return new \ExampleImplementation();   
    // now that there is a service registered for \ExampleInterface, we can actually use it
    var_dump($container->getInstance(\ExampleInterface::class)); // will dump an instance of \ExampleImplementation
     * A simple class with a constructor that allows to inject an instance of \ExampleInterface
    class Foobar 
        protected $example;

        public function __construct(\ExampleInterface $example)
            $this->example = $example;
        public function dumpExample()
    $object = $container->getInstance(Foobar::class); // this line would cause $object to be an instance of Foobar
                                                      // having a protected member $example, that holds a reference
                                                      // to the \ExampleImplementation
    $object->dumpExample(); // will dump an instance of \ExampleImplementation