Zend Framework 1 & 2

Integrates in Zend Framework 1 & 2.

Open

Integrates easily to your project or any framework.

Designed to work along with other containers.

Symfony 2

Integrates in Symfony 2 and works along with Symfony's default container.

New blog post: PHP-DI 4.1 released

Practical

class Foo
{
    private $bar;

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

Covers 80% of the use cases for 0 configuration!

Type-hint your constructors, and the container can guess which dependencies to inject.

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

}
Annotations

Makes your life so easy in your controllers!

Mark a property to be injected using an annotation. Particularly useful in controllers, read below.

<?php // PHP 5.6 example
return [
    Bar::class => factory(function () {
        return new Bar();
    }),

    Foo::class => object()
      ->constructor(link(Bar::class)),
];
PHP configuration

For full control!

Enjoy the powerful configuration API, in PHP so that you don't have to learn a YAML or XML specific format.

Features are à la carte: mix them all, disable annotation support, do whatever you want…

Powerful

Behind the simplicity, PHP-DI is meant to be extremely powerful.

Basic features
  • Constructor, method and property injection
  • Injection of objects and scalar values
  • Map interfaces to implementations
  • Recursive dependency injection
The power behind definitions
  • Definitions using the fluent API
  • Definitions using closures
  • Lazy injection
  • Clear definition overriding
Not just a container
  • Injection on an existing instance
  • Can behave like a factory
  • Integrates with other containers
  • Offers extension points

Go and have a look in the complete documentation.

Modern

PHP-DI reinvented itself for v4.0. Its configuration format has been thought through for the future: PHP 5.4, 5.5 and beyond!

We dropped the YAML format because we do no longer believe it's the most appropriate since:

PHP 5.4

Short arrays:

return [


];
PHP 5.5

Class name resolution via ::class:

return [
    Foo::class => DI\object(),

];
Future PHP 5.6

Import functions:

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

Furthermore, using PHP instead of YAML comes with other advantages:

  • Define services using callbacks (à la Pimple), because seriously, we are PHP developers, let's write some PHP
  • IDE support (auto-completion, refactoring, …)
  • You can actually use PHP code like constants, operations, … (instead of writing a custom language that developers have to learn)

Yes, PHP-DI is still compatible with PHP 5.3 (but seriously, upgrade!)

Fast

Lazy-loading

Are you injecting a service that is expensive to build?

Just mark it as lazy. A lightweight proxy will be injected instead, and the service will be created only if it is really used.

Learn more »

Configuration caching

Autowiring (reflection), annotations or PHP configuration have the same impact.

Every definition is cached so that is it not recomputed on each request.

Learn more »

Container compilation

Woops, that's not there yet! It's coming soon!

Let's compile all those abstract "service" definitions to pure PHP code. It will be almost as fast as if there was no container.

Learn more »

Friendly to framework developers

PHP-DI not only provides the classic API of a container, but also advanced features that makes it really practical when building or extending a framework.

Get & Has

PHP-DI implements ContainerInterface from Container-Interop 1.0. That means that you can use it in any Container-Interop compatible library or framework.

That universal interface standardizes the get and has methods:

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

Sometimes, you want to create an object with specific parameters and parameters coming from the container.

To do just this, PHP-DI offers the make method:

$container->make($name, $parameters);

Any constructor parameter that is not in the $parameters array will be resolved from the container.

For this, the container implements a separate DI\FactoryInterface that makes it easy to inject and use without tying your code to the container itself.

Inject on a instance

This is a very special feature but sometimes you have no choice: you have an object that is already created and you want to have all its dependencies injected. Of course, this excludes constructor injection, but the object might be a candidate for injection through setters or properties.

PHP-DI's container provide a injectOn method to do that:

$container->injectOn($instance);