2
3
4
5
6
7
8
9
10
11
12
##class | LoliGirl {
}
class Boy {
protected $girl ;
public function __construct() {
# $this
->girl = newLoliGirl();
}
}
One day Boy fell in love with Sister Yu....Boy is so annoying. . .
Do you feel bad? Every time I meet someone who treats me sincerely, I have to torture myself like this. . .
Boy said, I want to become stronger. I don’t want to be changed over and over again!
Okay, let's make Boy stronger:
##123456789101112131415 1617181920212223 2425
|
interface Girl {
// Boy need knows that I have some abilities.
}
class LoliGril implement Girl {
// I will implement Girl's abilities.
}
class Vixen implement Girl {
// Vixen is definitely a girl, do not doubt it.
}
class Boy {
##protected $girl ;
public function __construct(Girl $girl ) {
$this ->girl = $girl ;
}
}
$loliGirl = new LoliGirl();
$vixen = new Vixen( );
$boy = new Boy( $loliGirl ); <div class="line number25 index24 alt2">
<code class="php variable">$boy = new Boy( $vixen );
|
Boy I’m so happy that I can finally experience a different life without opening myself up. . . So Happy!
Dependency injection method
1. Constructor injection
1
2
3
4
5
6
7
8
|
##<?php
##class Book {
private $db_conn ;
public function __construct( $db_conn ) {
$this ->db_conn = <code class="php plain">$db_conn ;
##}
}
|
2、setter 注入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?php class Book {
private $db ;
private $file ;
function setdb( $db ) {
$this ->db = $db ;
}
function setfile( $file ) {
$this ->file = $file ;
}
}
class file {
}
class db {
}
class test {
$book = new Book();
$book ->setdb( new db());
$book ->setfile( new file());
}
|
Summary:
Because most applications are composed of two or more classes that cooperate with each other to implement business logic. Each object needs to obtain a reference to the object it cooperates with (that is, the object it depends on). If this acquisition process is implemented by itself, the code will be highly coupled and difficult to maintain and debug.
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 of IOC
Inversion Of Control (IOC)
Inversion of Control is a concept in object-oriented programming A design principle that can be used to reduce coupling between computer codes. The most common method is called Dependency 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.
##1234567891011121314
|
#<?php class Ioc {
protected $db_conn ; public static function make_book() {
# $new_book ->set_db(self:: $db_conn );
//...
//Other dependency injection
##
##
}
At this time, if you want to obtain a book instance, you only need to execute $newone = Ioc::makebook();
The above is a specific instance of container. It is best not to To write a specific dependency injection method, use registry to register, and get is better.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<?php
class Ioc {
*/
##protected static$registry = array ();
/**
#* Add a resolve (anonymous function) to the registry array ## *
* @param string $name Dependency identifier
* @param Closure $resolve An anonymous function used to create instances
public static function register( $name , Closure $resolve ) {
static :: $registry [ $name ] = $resolve ;
}
* @param string $name The identifier of the dependency
* @return mixed
* @throws \Exception
*/
public static function resolve( $name ) {
if ( static ::registered( $name )) {
$name = static :: $registry [ $name ];
return $name ();
}
throw new \Exception( "Nothing registered with that name" );
}
/** * 查询某个依赖实例是否存在
## ##* @return bool */ public
function registered( $name ) { ## return array_key_exists ( $name , static :: $registry );
}
##}
##Now you can register and inject a
as follows |
1 2 3 4 567891011<?phpIoc::register("book" | , function () { $book = new Book();
$book ->setdb( 'db' );
$book ->setfile( 'file' );
return $book ;
##});
//Inject dependencies
$book = Ioc::resolve( 'book' );
Summary of questions
1. Who are the participants?
Answer: Generally there are three parties, one is an object; one is the IoC/DI container; the other is an external resource of an object. Let me explain the nouns again. An object refers to any ordinary Java object; the IoC/DI container simply refers to a framework program used to implement IoC/DI functions; the external resources of the object refer to the object. Needed, but obtained from outside the object, are collectively referred to as resources, such as: other objects needed by the object, or file resources needed by the object, etc.
2. Dependence: Who depends on whom? Why are there dependencies?
Answer: An object depends on the IoC/DI container. Dependencies are inevitable. In a project, there are various relationships between various classes, and it is impossible for them all to be completely independent, which forms dependencies. Traditional development is to call directly when using other classes, which will form strong coupling, which should be avoided. Dependency injection borrows containers to transfer dependent objects to achieve decoupling.
3. Injection: Who injects into whom? What exactly is injected?
Answer: Inject the external resources needed into the object through the container
4. Inversion of control: Who controls whom? Control what? Why is it called reversal?
Answer: The container control object of IoC/DI mainly controls the creation of object instances. Reversal is relative to positive direction, so what counts as positive direction? Think about the application under normal circumstances. If you want to use C inside A, what would you do? Of course, the object of C is created directly, that is, the required external resource C is actively obtained in class A. This situation is called forward. So what is reverse? That is, class A no longer actively obtains C, but passively waits for the IoC/DI container to obtain an instance of C, and then injects it into class A in reverse.
5. Are dependency injection and inversion of control the same concept?
Answer: As can be seen from the above: Dependency injection is described from the perspective of the application. Dependency injection can be described completely: the application depends on the container to create and inject what it needs External resources; and inversion of control is described from the perspective of the container. The complete description is: the container controls the application, and the container reversely injects the external resources required by the application into the application.
|
|
|