The Aura.Di package provides a serializable dependency injection container with the following features:
constructor and setter injection
inheritance of constructor parameter and setter method values from parent classes
inheritance of setter method values from interfaces and traits
lazy-loaded instances, services, includes/requires, and values
optional auto-resolution of typehinted constructor parameter values
Fully describing the nature and benefits of dependency injection, while desirable, is beyond the scope of this document. For more information about "inversion of control" and "dependency injection" please consult http://martinfowler.com/articles/injection.html by Martin Fowler.
Finally, please note that this package is intended for use as a dependency injection system, not as a service locator system. If you use it as a service locator, that's bad, and you should feel bad.
The intent behind Aura.Di is for it be used like so:
Instantiate a container.
Do all configuration for all classes and services.
Lock the container so it cannot be modified further.
Retrieve objects from the locked container.
Note that calling
newInstance() will automatically lock the container, preventing further configuration changes. This is true even inside configuration code, so use the
lazy*() methods instead while configuring the container.
We instantiate a Container like so:
use Aura\Di\ContainerBuilder; $builder = new ContainerBuilder(); $di = $builder->newInstance();
The most straightforward way is to create an object through the Container is via the
$object = $di->newInstance('Vendor\Package\ClassName');
N.b.: The Container locks itself once a new instance is produced; this ensures that the Container configuration cannot be modified once objects have been created.
However, this is a relatively naive way to create objects with the Container. It is better to specify the various constructor parameters, setter methods, and so on, and let the Container inject those values for us only when the object is used as a dependency for something else.