Php dependency injection and control inversion, php injection inversion. Php dependency injection and control inversion, php injection inversion DIDependencyInjection dependency injection IoCInversionofControl to control inversion to understand the above two concepts, we must clarify the following: php dependency injection and control inversion, php injection inversion
DI -- Dependency Injection
IoC -- Inversion of Control Inversion
To understand the two concepts above, you must clarify the following issues:
1. Who are the participants?
A: Generally, there are third-party participants. one is an object, the other is an IoC/DI container, and the other is an external resource of an object. The term also explains that an object refers to any and common Java Object. the IoC/DI container simply refers to a framework program used to implement the IoC/DI function; the external resource of an object refers to what the object needs, but is obtained from outside the object. all resources are collectively referred to as resources, such as other objects required by the object or file resources required by the object.
2. Dependency: who depends on whom? Why is there dependency?
A: an object depends on the IoC/DI container. Dependency is inevitable. in a project, there are various relationships between classes and it is impossible to be completely independent. this forms dependency. Traditional development calls other classes directly, which forms a strong coupling, which is to be avoided. Dependency Injection borrowed containers to transfer the dependent objects for decoupling.
3. injection: Who is injected? What is injection?
A: inject the required external resources to the object through the container.
4. Control reversal: who controls who? What is the control? Why is reverse?
A: The IoC/DI Container Control object mainly controls the creation of object instances. Reverse is relative to positive, so what is positive? What do you do if you want to use C in A conventional application? Of course, it is to directly create the C object, that is, actively obtain the required external resource C in Class A, which is called positive. So what is reverse? That is, Class A does not take the initiative to obtain C, but passively waits, waits for the IoC/DI container to obtain a c instance, and then injects it into Class A in reverse direction.
5. is dependency injection the same concept as control reversal?
A: From the above, we can see that dependency injection is described from the application perspective. you can describe the complete dependency injection point: the application depends on the container to create and inject the external resources required by it; the control inversion is described from the container perspective. The Complete description is: The container controls the application, and the container reversely injects the external resources required by the application to the application.
The following example shows how to implement dependency injection:
1. constructor injection
db_conn = $db_conn; }}
2. setter injection
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());
}?>
The code of the above two methods is clear, but when we need to inject many dependencies, it means that we need to add many lines and it will be difficult to manage them.
A better solution is to create a class as the container of all dependencies. in this class, you can store, create, obtain, and find the required dependencies.
Set_db (self: $ db_conn); //... // Other dependency injection return $ new_book ;}}
To obtain a book instance, run $ newone = Ioc: makebook ();
The above is a specific instance of the container. it is best not to write a specific dependency injection method. it is better to use registry registration and get
You can register and inject
Setdb ('... '); $ book-> setprice ('... '); return $ book;}); // injection dependency $ book = Ioc: resolve ('book');?>
Reference: http://www.4wei.cn/archives/1002316
DIDependency Injection dependency Injection IoCInversion of Control inversion to understand the above two concepts, you must clarify the following...