Understanding php dependency injection and inversion of control_php skills

WBOY
Release: 2016-05-16 08:59:46
Original
3140 people have browsed it

to understand the two concepts of php dependency injection and inversion of control, you must understand the following issues:

di——dependency injection

ioc——inversion of control

1. who are the participants?

answer:generally there are three parties, one is a certain object; one is the ioc/di container; the other is an external resource for 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, and dependency injection can be described completely point: the application relies on the container to create and inject the external resources it needs; and inversion of control is described from the perspective of the container. the complete description: the container controls the application, and the container reversely injects the application needs into the application. external resources.

let’s take a closer look at some implementation methods ofdependency injectionthrough examples:

1.constructor injection

db_conn = $db_conn; } }
Copy after login

2. setter injection

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()); } ?>
Copy after login

the code of the above two methods is very clear, but when we need to inject many dependencies, it means adding a lot of lines, which will be more difficult to manage.

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

set_db(self::$db_conn); //... //... //其他的依赖注入 return $new_book; } }
Copy after login

at this time, if you want to obtain a book instance, you only need to execute $newone = ioc::makebook();

the above is a specific example of container. it is better not to write a specific dependency injection method. it is better to use registry registration and get acquisition


         
Copy after login

now you can register and inject one through the following methods

setdb('...'); $book->setprice('...'); return $book; }); //注入依赖 $book = Ioc::resolve('book'); ?>
Copy after login

the above is the understanding of php dependency injection and inversion of control. i hope it will be helpful to everyone in learning php programming.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!