Before learning yii2, I already knew the term "dependency injection" and tried to understand it. However, it was always in the fog, and it may have never been possible to actually use it, so I don't know much about it. this time I learned yii2, and I will handle this so-called "high school !!! Haha, before learning yii2, he already knew the term "dependency injection" and tried to understand it. However, it was always in the fog of the cloud and may have never been a real chance to use it, so I don't know much about it. this time I learned yii2, and I will handle this so-called "high school !!! Haha
Before we start "dependency injection", let's talk about the design model. I think it may be faster to understand from this aspect.
At the very beginning, we learned "procedural" programming, for example, implementing certain functions, from top to bottom, at most, some general functions are sorted out as some general functions ). Then we learned about "object-oriented", followed by How to apply the concept of "object" to design better programs (various design patterns ), "Dependency injection" is also a design mode. Its implementation solves our concerns about "front-and-back dependencies" or "context dependencies" when implementing functions. This sentence sounds hard to understand. let's explain it briefly below.
How to understand "before and after dependency"
For example, if we want to create a BMW and a Porsche separately, we must strictly abide by the following sequence when making a BMW (1 and 2 may not be in a fixed order ):
1. find the BMW factory (require or include)
2. prepare the preparation before making the BMW (Initialize the parameters of the object. if the parameters are objects, some parameters may also need to be prepared ...)
3. a "BMW" (new BaoMa ($ params1, $ params2…) can be generated only after both 1 and 2 are complete ....)), That is, 1 and 2 must be before 3.
The same process is required for making a "Porsche". of course, when creating other cars, you have to go through this process. This is the issue of "pre-and post-dependency. Is there a way to sort out the process of implementing a vehicle separately for general purpose? That is to say, first prepare the implementation process, and then provide "raw materials". to implement anything, it depends on what "raw materials" you provide (dependency injection ).
The above is the yii2 article (7) dependency injection -- (1) What is the content of dependency injection. For more information, see The PHP Chinese website (www.php1.cn )!