Get the App
SLTechnology News&Howtos  ›  Development  › 

How to apply php dependency injection

Shulou Source: shulou.com Published: 2022-05-31 23:06:05 10月04日 Update

The knowledge of this article "php dependency injection how to apply" is not quite understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "php dependency injection how to apply" article.

What is dependency injection (DI)

Dependency injection (DI) is essentially a dependency on a class that is automatically injected through a constructor.

Generally speaking, you are currently operating a class, but some of the methods or functions of this class can be accomplished not only by this class, but by another class.

The most direct sign is when the parameter data is passed as the object. Strictly speaking, you want to operate on another class in another class, and the two classes form an interdependent relationship, and the way to pass parameters is called injection.

Second, the reasons for the emergence of dependency injection

In the beginning, when php needs to use another class in one class, it will do the following

For example, if I need to use the adapter class in the container class, I need to instantiate it before using it.

If you need to use a large number of external classes, this will result in a high degree of coupling, which can easily lead to later maintenance difficulties.

Popularly speaking, container can not work without external classes, which is called too high coupling.

Tags: Objects times applications content parameters instances containers methods confusion that is articles coupling high-order magic coupling popularity knowledge articles resources questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS MariaDB Docker Linux