Direct access to the container's content is implemented in several ways. To choose the appropriate method suitable for coding a specific project, it is necessary to consider the pros and cons of each approach, as well as their testing options.
Classes inherited from the Hleb\Base\Container class gain additional capabilities in the form of methods and the $this->container property to access services. The standard framework classes — controllers, middlewares, commands, events — are already inherited from this class.
If a service in the container interface has its own method assigned, the service can be accessed through this method. Example of accessing a demo service in a controller:
The reference to the container is stored in the $this->config property (key 'container' in the array) of the object class inherited from Hleb\Base\Container.
When creating the specified object, a different value can be assigned (for example, with a test container) in the 'config' argument.
Otherwise, if a specific container is not specified in the 'config' argument or the 'config' argument of the constructor is missing, the container will be created by default.
Exceptions are the Model classes, where accessing the service similarly will be as follows:
Access to the service container is also provided by the Hleb\Static\Container class, for example:
In the /vendor/phphleb/framework/Static/ folder, there are wrapper classes over the framework's standard services, which can be used in code similarly to the Hleb\Static\Container class, but for individual services.
These services can also be accessed using the previously mentioned methods.
Due to the existence of different approaches in naming interfaces, accessing standard services from the container can be either with or without the Interface suffix. For example, Hleb\Reference\RequestInterface is equivalent to Hleb\Reference\Interface\Request.
← Container Structure Dependency Injection →