Inject on an existing instance

Feature introduced in PHP-DI 3.3

The standard way of using a container is to get an object from it, with all its dependencies injected:

$object = $container->get('foo');

But in some situations, you don't have the control of the creation of an object, yet you want to resolve its dependencies.

PHP-DI offers the injectOn method:

// $object is an instance of some class

$container->injectOn($object);

Now, $object has all its dependencies injected (through setter injections and property injections).

Constructor injection

PHP-DI will not perform any constructor injection (because the instance is already created).

If you create the object yourself, you'll have to do the constructor injection yourself:

$object = new MyClass($someDependency, $someOtherDependency);
$container->injectOn($object);

If you get the object from some library/framework, then just call injectOn()

Why?

Hopefully, that will help to integrate PHP-DI with other frameworks:

Example:

class MyController {
    public function __construct() {
        // get container ...
        $container->injectOn($this);
    }
}

Of course, the preferred method is still to use $container->get(). But sometimes you can't get to the root of the framework to intercept the creation of your objects.