Home  >  Article  >  PHP Framework  >  What is the use of yii dependency injection?

What is the use of yii dependency injection?

藏色散人
藏色散人Original
2020-07-22 11:53:112933browse

yii Dependency injection is to put dependent classes into the container and parse out instances of these classes. The purpose is to achieve decoupling of classes, and "dependency injection" is also an implementation of a design pattern. The implementation solves our concern about "backward and backward dependencies" or "contextual dependencies" when implementing functions.

What is the use of yii dependency injection?

yii dependency injection:

Put dependent classes into the container and parse out the dependencies of these classes An example is dependency injection. The purpose is to achieve decoupling of classes.

Recommended: "yii Tutorial"

Introduction:

Before starting "Dependency Injection", we Let’s talk about design patterns first. I think starting from this aspect may help you 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. Only after 1 and 2 are fully prepared can you generate "BMW" (new BaoMa($params1,$params2….)), 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 the detailed content of What is the use of yii dependency injection?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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