Dependency Injection in Jet framework

What is DI actually good for?

If you already know this, you can skip this chapter ;-)

The main advantage of DI is that it completely isolates user classes from the creation and initialization of their services. This is the main and most important advantage. But it will be better to explain it on a specific case.

Suppose we have logging in the system. Logging must be provided by a class. And logging is a service that this class provides - the class is the service provider.

We also have service users. So in our case, anything that needs to log to some log. Again, let's be specific and say it can be a controller (but it can be anything else, of course). In a poorly designed application that completely ignores DI, that controller would log like this:

class Controller extends Controller_Abstract {
    
    public function 
someAction() : void
    
{
        
//... ... ...
        
$logger = new Logger('~/path/to/some/log/dir');
        
$logger->logEvent$event$event_data );
        
//... ... ...
    
}
}

This is absolutely wrong and this is not the way to develop software. Why? For purely objective reasons. And then the day comes when you need to change the logging:

  • Maybe replace that class with a completely different one
  • Or reconfigure the class, change the service parameters
  • Or let some higher authority decide how the application will log.
None of this will come easily to you. You will have to go through the whole project, modify many places in the code (hundreds of them), and of course risk errors and problems. A nightmare for a larger project.

Not to mention the simple fact that the controller in our example simply does what it is not supposed to do. He takes care of initializing the service and that is not his competence and responsibility. This is again a transgression against the rules for creating sustainable projects.

But I would say that the main scare is the idea that for example in two years of project operation you have to go through and edit hundreds of places in the project, even just for a small partial change. That's obviously an objective big problem and we want to avoid that.

This is the main benefit of DI mechanisms. When it is followed, it is ensured that the initialization of the service is not taken care of by the service user.

DI is therefore a necessary thing.

DI mechanisms in PHP Jet

There are a number of mechanisms to implement DI. Each mechanism is suitable for a different situation. PHP Jet offers the following three mechanisms, which are implemented in it and used in the sample application.

  • Service guarantors
    Service guarantors are the basic approach to how PHP Jet handles system services such as the aforementioned logging, authentication and authorization, and many others (see the relevant chapter). The goal is to make the use of system services as simple as possible while taking advantage of DI principles.
  • Application services
    A simple but very effective system for creating and using services at the application level, possibly using application modules.
  • Factories
    The principle of factories is used in PHP Jet to instantiate system classes and, above all, to allow them to be replaced and extended by classes created by the developer.
Previous chapter
Attributes - Jet\Attributes
Next chapter
Service Guarantor