Dependency Injection (also DI) is a framework mechanism for supplying dependencies to the constructor or other methods of created objects.
When the framework creates objects such as controllers, middlewares, commands, and others, dependency injection is already set up when the target method (including the constructor) is called.
According to the DI mechanism, if you specify the necessary classes or interfaces in the method's dependencies (arguments), the framework will attempt to find such matches in the container, retrieve them from the container, or create the object itself and substitute it in the required argument.
If such a service is not found in the container, an attempt will be made to create an object from a suitable class in the project, and if the latter has dependencies in its constructor, the framework will try to fill them in a similar way.
If there are no substitution values for arguments with default values, the default will be used.
Otherwise, the framework will return an error indicating that the DI for the specified dependencies could not be successfully used.
When a controller or middleware object is created on the framework side, the constructor's dependencies are resolved first, then those of the called method.
Also, when a request is processed by the framework, only one method in the matched controller will be called. In such a case, it doesn't matter where the dependency comes from, whether from the constructor or method, although in some cases, the constructor is more convenient.
The following example shows two controller methods with different assignments of $logger from the container via DI.
Dependencies for middleware are set in a similar manner.
In the framework commands and events (Events), this is implemented in a similar way, but only through the constructor:
Dependency injection is convenient because during testing, we can create the necessary values for class dependencies. However, when creating an object manually, initializing all its dependencies ourselves would be inconvenient. To automate this process, the framework provides the Hleb\Static\DI class.
This section demonstrates how to create an object of a class whose constructor has a dependency, and how to call the desired method of the object where a value also needs to be automatically inserted. The example also shows a dependency that is not from the container (the Insert class), whose object is created and injected into the method.
A frequently used variant of DI with Request and Response (in this case obtained from the container):
Due to various approaches in interface naming conventions, obtaining standard services from the container may involve interfaces ending with Interface or not. For example, Hleb\Reference\RequestInterface is equivalent to Hleb\Reference\Interface\Request.
← Retrieving Service Request →