PHP-DI 5

The dependency injection container for humans

Get started Documentation

Autowiring

Type-hint your constructor parameters and the container can guess which dependencies to inject.

class Foo
{
    private $bar;

    public function __construct(Bar $bar)
    {
        return $this->bar = $bar;
    }
}

Covers 80% of the use cases for 0 configuration.

Mark a property to be injected using a annotations. Particularly useful in controllers, CLI command, system tests…

class Foo
{
    /**
    * @Inject
    * @var Bar
    */
    private $bar;

}

Autowiring and annotations are optional and can be disabled.

Expressive configuration

PHP-DI's configuration is written in PHP, allowing to benefit from auto-completion, static analysis and refactoring support with any IDE.

Object creation

return [
    Foo::class => object()
        ->constructor(
            get(Bar::class)
        )
];

Inline factories

return [
    Foo::class => function ($container) {
        $dependency = $container->get(Bar::class);
        return Foo::create($dependency);
    }
];

Interface-to-implementation binding

return [
    NotifierInterface::class => get(EmailNotifier::class)
];

Environment variables

return [
    'db.host' => env('DB_HOST', 'default value')
];

The configuration format has been imagined as a successor of XML and YAML, taking advantages of all the latest features of PHP:

PHP 5.4 short arrays

return [


];

PHP 5.5 ::class magic constant

return [
    Foo::class => DI\object()
      ->constructor(DI\get(Bar::class))
];

PHP 5.6 use function

return [
    Foo::class => object()
      ->constructor(get(Bar::class))
];

Scales with your application

From simple applications

Just create the container and you are good to go thanks to autowiring.

$container = ContainerBuilder::buildDevContainer();

$home = $container->get(HomeController::class);

You can also define services à la Pimple:

$container->set('db', new Database($options));

$container->set('logger', function () {
    $logger = new Monolog\Logger();
    $logger->pushHandler(new StreamHandler('app.log'));
    return $logger;
});

$db = $container->get('db');
$logger = $container->get('logger');

To complex and modular systems

Register as many configuration files as you want.

// base config
return [
    'notifiers' => [
        get(EmailNotifier::class),
    ]
];

Use definition overriding to extend lists, decorate or replace previous entries…

// module (aka bundle) config
return [
    'notifiers' => add([
        get(TextNotifier::class),
        get(MobilePushNotifier::class),
    ])
];

Powerful container API

PHP-DI provides the classic API of a container as well as advanced features useful to build or extend a framework.

Get & Has

PHP-DI is compliant with container-interop:

$container->get($name);
$container->has($name);

Make

Use the container as a factory:

$parameters = [ ... ];
$container->make(Foo::class, $parameters);

Missing constructor parameters will be resolved from the container.

Invoke a callable

Dependency injection in callables, à la AngularJS:

$container->call(function (Logger $logger, EntityManager $em) {
    // ...
});

Integrations

PHP-DI integrates easily in your application or in frameworks.

No framework? It works too, have a look at this demo application.