V
V
vrazbros2015-10-04 14:39:01
PHP
vrazbros, 2015-10-04 14:39:01

What is DI Dependency Injection?

How to briefly describe the essence of DI dependency injection? It would be desirable in relation to php but not essentially. Everywhere I come across a description of patterns and articles of 3 pages, but how to say the essence of this term in a few sentences?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Koryukov, 2015-10-04
@vrazbros

Dependency injection is when you don't hard-code in the code that one component uses another, but you pass one component to another.
Specifically:

// без DI
class Application{
     private $database;
     public function start(){
          $this->database=new Database("localhost","user","pass");
          //....
     }
}
$application = new Application();
$application->start(); 

// с DI
class Application{
     private $database;
     public fucnction __construct(Database $d){
           $this->database=$d;
     }
     public function start(){
          //....
     }
}
$application = new Application(new Database("localhost","user","pass"););
$application->start()

Thus, the creation of most components is concentrated in one place - where the application starts.
What for?
First, it's easy to control. If we want to use another class instead of Database to work with the database, we know where and what to change.
Secondly, it is convenient to test, this follows from the first - for unit testing, we will probably need to use a Mock object instead of a Database. Well, the ease of replacing the component is only in our favor.

A
Alexander Fedotov, 2015-10-06
@orderbynull

Let me explain with a real life example.
You buy a Zhiguli, drive it home, open the soot and see, oh my God, the engine is tightly welded to the body. That is, it is not on bolts, not on mounts that can be quickly removed - but simply sewn into the body. You thought that I would buy a car and put a BMW engine in it. To do this, you now need to turn the entire engine compartment and literally uproot the engine from there, and manage not to damage other systems.
In this case, the motor is sewn into the car, the so-called strong dependence. DI (dependency injection) in this example is if the guys at the factory provided for an easy replacement of the engine, that is, unscrewed the fasteners, disconnected the hoses - that's it, you can get the engine and put another one.
Now for PHP. The factory guys are you. The car is your web application. An engine is some kind of dependency of your web application.
Total - the idea is that some components are weakly dependent on others. The application must be written in such a way that one part can be replaced by another without difficulty.

S
sanex3339, 2015-10-04
@sanex3339

DI is very cool, last week I implemented it for the first time in my project. Now it’s a pleasure to work with dependencies, and yes, it’s convenient to mock dependencies in tests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question