yii2 Essay (7) Dependency Injection - (1) What is Dependency Injection

黄舟
Release: 2023-03-04 21:14:01
Original
1036 people have browsed it

Before learning yii2, I already knew the term "dependency injection" and tried to understand it, but it was always in the clouds. Maybe I never had the opportunity to use it in real life, so I didn't understand it thoroughly. , this time I learned yii2 and solved this so-called "high-level" problem! ! ! Haha

Before starting "dependency injection", let's talk about design patterns first. I think starting from this aspect may help us understand it faster.

When we first learned programming, we learned "procedural" programming. For example, to implement certain functions, we would implement them sentence by sentence from top to bottom. At most, we would organize some common functions. Come out as some general packages (functions). Then we learned about "object-oriented", and what followed was how to apply the concept of "object" to design better programs (various design patterns), and "dependency injection" is also an implementation of a design pattern. Its implementation solves our concern about "backward and backward dependencies" or "contextual dependencies" when implementing functions. This sentence sounds difficult to understand, so let’s briefly explain it.

How to understand "front and back dependence"

For example, we want to build a "BMW" and a "Porsche" respectively. When manufacturing a BMW, we must strictly abide by the following order (1 , 2 can not be in a fixed order):

1. Find the BMW factory, (require or include)

2. Prepare the preparations before manufacturing BMW (initialize the parameters of the object, if the parameter is an object If so, you may also need to prepare some parameters. .)

3. "BMW" (new BaoMa($params1,$params2….)) must be prepared after both 1 and 2 are completed. That is, 1 and 2 must come before 3.

The manufacturing of "Porsche" requires the same process. Of course, this process must also be gone through when manufacturing other cars. This is the problem of "front and back dependence". Is there a way to sort out the process of realizing a car separately and make it universal? That is to say, prepare the implementation process first, and then provide the "raw materials". What you want to implement depends on the "raw materials" (dependency injection) you provide.

The above is yii2 Essay (7) Dependency Injection - (1) What is the content of dependency injection? For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
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
Popular Tutorials
More>
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!