A dependency injector container with lazyload.

1.1.1 2021-03-30 14:49 UTC

This package is auto-updated.

Last update: 2021-04-20 09:04:23 UTC


pipeline status coverage report


quadrixo/dependency-injection is a dependency injection container with lazyload.

Get Started

This container can be used without configuration, just with the lazyload.

class Foo {}

class Bar {
    public function __construct(Foo $foo) {}

$container = new Quadrixo\Container();
$bar = $container->get(Bar::class);


The container can be customized and build from an instance of Quadrixo\ContainerBuilderInterface. This package provides a default implementation of this interface Quadrixo\DefaultContainerBuilder.

$builder = new Quadrixo\DefaultContainerBuilder();
// ...
$container = $builder->buid();

Type Implementation

Map an implementation to a type :

interface FooInterface {}

class Foo implements FooInterface {}

$builder->add(FooInterface::class, Foo::class);
// ...

$foo = $container->get(FooInterface::class);
assert($foo instanceof Foo);


Map a type to an object :

$foo = new Foo();
$builder->add(FooInterface::class, $foo);


The returned instance can be created with a factory. The factory takes as argument an instance of Psr\Container\ContainerInterface and return the desired object.

    function(ContainerInterface $container)
        $object = new Foo($container->get(Bar::class));
        return $object;


Get with an alias instead of the classname :

$builder->add(\Some\Complex\Namepsace\To\The\FooInterface::class, \Another\Complex\Namepsace\To\The\FooImplementation::class)
    ->alias('foo', \Some\Complex\Namepsace\To\The\FooInterface::class);
// ...

$foo = $container->get('foo');
assert($foo instanceof \Another\Complex\Namepsace\To\The\FooImplementation);

Due to the lazyload, an alias can be mapped to an undeclared instanciable type.

Instance Configuration

The returned object can be configured be getting it.

$builder->add(FooInterface::class, Foo::class)
    ->configure(function($foo) {});

Due to the lazyload, configuration can be used with an undeclared instanciable type.


By default, returned object are considered as singleton. Each call to $container->get('foo') will return the same instance.

But a type can be declared as transient, and reinstanciated each times.

$builder->add(FooInterface::class, Foo::class, false);

A type mapped to an object is always considered as singleton.