PHP-DI 7

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)
    {
        $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()
        ->constructor(
            get(Bar::class)
        )
];

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 [
    '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\create()
      ->constructor(DI\get(Bar::class))
];

PHP 5.6 use function

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

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' => [
        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),
    ])
];

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:

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

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

Integrations

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

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

Learn more

Sponsors

PHP-DI is sponsored by these companies:

JetBrains Null Amezmo Web-ID ActiveCollab

Become a sponsor