A PSR-11 compatible container with lazyload.

3.1.0 2022-04-03 08:43 UTC

This package is auto-updated.

Last update: 2022-05-03 06:48:41 UTC


pipeline status coverage report


quadrixo/dependency-injection is a PSR-11 compatible 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\DI\ContainerBuilder())->build();
$bar = $container->get(Bar::class);

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.


Before returning an object instance, it can be configured.

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

Also works with alais and id.

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

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


By default, value are resolved just once (singleton), but can be resolved each time (transient).

To declare a value resolution as transient, use the third arguments $isSingleton with false value.

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