This article brings you relevant knowledge aboutPHP, which mainly introduces issues related to dependency injection, including what is dependency injection, the reasons for dependency injection, and the application of dependency injection, etc. Wait, I hope it helps everyone.
Recommended study: "PHP Video Tutorial"
One article to understand PHP dependency injection. Many people will hear the term dependency injection after learning PHP for a period of time, but they have only a little understanding of it. I understand that dependency injection is actually a PHP programming design pattern, although it has not been included in the design pattern. Design patterns exist for the efficiency of programming, and dependency injection certainly does.
1. What is Dependency Injection (DI)
II , The reason why dependency injection appears
- At the beginning, when PHP needs to use another class in one class, it will do the following operations
- For example, when I use container If the adapter class needs to be used in a class, it needs to be instantiated before use.
- If a large number of external classes need to be used, this will cause the coupling degree to be too high, which can easily cause later maintenance difficulties
- In layman's terms, it means that the container cannot work without external classes, which is called too high coupling
adapter = new adapter(); } }
Copy after login
3. Simple dependency injection
- The coupling degree of the above code is too high, which leads to the emergence of dependency injection, mainly to understand the coupling
- As shown below, we only need to pass in the class object that needs to be operated.
- The parameters of the dependency injection operation are objects, not ordinary parameters. Do you have a better understanding?
- But such a simple dependency injection will cause if you depend on many classes, you It will take a long time to pass parameters and it will be easy to get confused
adapter = $adapter; } }
Copy after login
4. High-level dependency injection
- In order to solve the problem of parameter confusion above, At this time, dependency injection has evolved
- Through the magic method, __get sets the object
- If you know a little about magic methods, please refer to my previous article: Detailed explanation of magic methods in php
- At this time, we can solve the problem of too many dependencies and confusing parameters
instance[$name] = $value; } } $container = new container(); $container->adapter = new adapter();
Copy after login
5. Application of dependency injection
- We first define a container class, which is mainly used to inject the classes you want to operate into the container. When using it, you only need to pass the container object.
-
instance[$name] = $value; } } class adapter { public $name = '我是调度器'; } $container = new container(); $container->adapter = new adapter(); class autofelix { private $container; public function __construct(container $container) { $this->container = $container; } public function who($class) { return $this->container->instance[$class]->name; } } $autofelix = new autofelix($container); $who = $autofelix->who('adapter'); var_dump($who); //我是调度器
Copy after login
6. High-order optimization
In the above application, we directly inject the instantiated objects into the container
- This will result in that all objects are still If it is not used, it will be instantiated again, causing resource loss
- We can pass in the closure so that the object will not be instantiated and injected. When you need to use it, instantiate it again.
- You can reduce the loss of server resources
-
adapter = new adapter(); //高阶优化 $container = new container(); $container->adapter = function () { return new adapter(); };
Copy after login
Recommended learning: "
PHP Video Tutorial
"
The above is the detailed content of PHP dependency injection summary sharing. For more information, please follow other related articles on the PHP Chinese website!