Home > Article > Backend Development > Understanding php dependency injection and inversion of control_php skills
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 of dependency injection through examples:
1.constructor injection
<?php class 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 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
<?php class ioc { protected $db_conn; public static function make_book() { $new_book = new book(); $new_book->set_db(self::$db_conn); //... //... //其他的依赖注入 return $new_book; } }
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
<?php class ioc { /** * @var 注册的依赖数组 */ protected static $registry = array(); /** * 添加一个resolve到registry数组中 * @param string $name 依赖标识 * @param object $resolve 一个匿名函数用来创建实例 * @return void */ public static function register($name, closure $resolve) { static::$registry[$name] = $resolve; } /** * 返回一个实例 * @param string $name 依赖的标识 * @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.'); } /** * 查询某个依赖实例是否存在 * @param string $name id * @return bool */ public static function registered($name) { return array_key_exists($name, static::$registry); } }
now you can register and inject one through the following methods
<?php $book = Ioc::registry('book', function(){ $book = new Book; $book->setdb('...'); $book->setprice('...'); return $book; }); //注入依赖 $book = Ioc::resolve('book'); ?>
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.