Home>Article>Backend Development> PHP Dependency Injection (DI) and Inversion of Control (IoC) Example Tutorial
To understand the two concepts of PHPDependency InjectionandInversion of Control, you must understand the following two issues:
DI —— Dependency Injection Dependency Injection
IoC —— Inversion of Control Inversion of Control
I can’t live without you, then you are my dependence. To put it bluntly:
is not my own, but it is what I need and what I rely on. Everything that needs to be provided externally requires dependency injection.
From the above code we can see thatBoy
strong dependencyGirl
must be injected into the instance ofGirl
during construction.
So why is there the concept ofDependency Injection
? What problem doesDependency Injection
solve?
Let’s modify the above code to the code we all wrote when we first started:
##1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class
Boy {
protected
$girl
;
public
function
__construct(Girl
$girl
) {
$this
->girl =
$girl
;
}
}
class
Girl {
...
}
$boy
=
new
Boy();
// Error; Boy must have girlfriend!
// Therefore, he must have a girlfriend Only friends
$girl
=
new
Girl();
$boy
=
new
Boy(
$girl
);
// Right! So Happy!
|
1
2
3
4
5
6
7
|
class
Boy {
protected
$girl
;
public
function
__construct() {
$this
->girl =
new
Girl();
##}
|
Boy
’s girlfriend has been hardcoded intoBoy’s body. . . Every time
Boyis reborn and he wants a different type of girlfriend, he has to strip himself naked.
One day
Boy
LoliGirland really wants her to be his girlfriend. . . what to do? Rebirth yourself. . . Uncover yourself. . . Throw
Girlaway. . . Put
LoliGirlinside. . .
3
4
5
6
7
8
9
10
11
12
##class |
LoliGirl {
}
class
Boy {
protected
$girl
;
public
function
__construct() {
#
$this
->girl =
new
LoliGirl();
}
}
One day Do you feel bad? Every time I meet someone who treats me sincerely, I have to torture myself like this. . .
Okay, let's make
Dependency injection method1. Constructor injection 2、setter 注入
That’s why we have the concept of dependency injection. Dependency injection solves the following problems:
The codes of the above two methods are very clear, but when we need to inject many dependencies, it means adding a lot of lines, which will be compared Unmanageable. A better solution is to create a class as the container for all dependencies. In this class, you can store, create, obtain, and find the required dependencies. Let’s first understand the concept ofIOC Inversion Of Control (IOC)Inversion of Controlis a concept in object-oriented programming A design principle that can be used to reduce coupling between computer codes. The most common method is calledDependency Injection(Dependency Injection, DI), and the other is called "Dependency Lookup" (Dependency Lookup). Through inversion of control, when an object is created, an external entity that controls all objects in the system passes the reference of the object it depends on to it. It can also be said that dependencies are injected into the object.
|
The above is the detailed content of PHP Dependency Injection (DI) and Inversion of Control (IoC) Example Tutorial. For more information, please follow other related articles on the PHP Chinese website!