Understand php dependency injection and control reversal, and php dependency injection reversal
To understand the two concepts of php dependency injection and control reversal, you must understand the following issues:
DI -- Dependency Injection
IoC -- Inversion of Control Inversion
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 container control object of IoC/DI 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. It can describe the complete point of dependency injection: 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
<?phpclass Book { private $db_conn; public function __construct($db_conn) { $this->db_conn = $db_conn; }}
2. setter Injection
<?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());}?>
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.
<? Phpclass Ioc {protected $ db_conn; public static function make_book () {$ new_book = new Book (); $ new_book-> 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
<? Phpclass Ioc {/*** @ var registered dependency array */protected static $ registry = array (); /*** Add a resolve to the registry array * @ param string $ name dependent identity * @ param object $ resolve an anonymous function to create an instance * @ return void */public static function register ($ name, closure $ resolve) {static: $ registry [$ name] = $ resolve ;} /*** return the ID of an instance * @ param string $ name dependent * @ return mixed */public static function resolve ($ name) {if (static :: registered ($ name) {$ name = static: $ registry [$ name]; return $ name ();} throw new Exception ('Nothing registered with that name, fool. ');}/*** query whether a dependent instance exists * @ param string $ name id * @ return bool */public static function registered ($ name) {return array_key_exists ($ name, static: $ registry );}}
You can register and inject
<? Php $ book = Ioc: registry ('book', function () {$ book = new book; $ Book-> setdb ('... '); $ book-> setprice ('... '); return $ book;}); // injection dependency $ book = Ioc: resolve ('book');?>
The above is an understanding of php dependency injection and control reversal. I hope it will be helpful for you to learn about PHP programming.