The dependency injection container for humans

Get started Documentation


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

class Foo
    private $bar;

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

Covers 80% of the use cases with 0 configuration.

Learn more

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 read more

return [
    Foo::class => create()

Inline factories read more

return [
    Foo::class => function ($container) {
        $bar = $container->get(Bar::class);
        return new Foo($bar);

Interface-to-implementation binding read more

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

Environment variables read more

return [
    '' => 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\create()

PHP 5.6 use function

return [
    Foo::class => create()

Learn more

Scales with your application

From simple applications

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

$container = new Container();

$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' => [

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

// module (aka bundle) config
return [
    'notifiers' => add([

Learn more

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 read more

PHP-DI is compliant with PSR-11:


Make read more

Use the container as a factory:

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

Missing constructor parameters will be resolved from the container.

Invoke a callable read more

Dependency injection in callables, à la AngularJS:

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

Learn more


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

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

Learn more


PHP-DI is sponsored by these companies:

JetBrains Null Amezmo Web-ID ActiveCollab

Become a sponsor