Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a lightweight dependency injection container with php

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use php to achieve a lightweight dependency injection container", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use php to implement a lightweight dependency injection container.

What is dependency injection?

IOC: English full name: Inversion of Control, Chinese name: control inversion, it is also called Dependency Injection (DI for short).

When an instance of one class needs the assistance of an instance of another class, in the traditional programming process, the caller usually creates the instance of the callee. In the way of dependency injection, the work of creating the callee is no longer done by the caller, so it is called control inversion, the work of creating the instance of the callee is done by the IOC container, and then the caller is injected, so it is also called dependency injection.

Take a simple example:

(1) in primitive society, there was almost no social division of labor. The person who needs the axe (the caller) can only sharpen the axe himself (the callee).

(2) after entering the industrial society, factories appeared. The axe is no longer made by ordinary people, but is produced in the factory, when the person who needs the axe (the caller) goes to the factory and buys the axe, regardless of the manufacturing process.

(3) to enter the "distribution according to demand" society, people who need an axe do not need to find a factory, sit at home and issue a simple instruction: need an axe. The axe naturally appeared in front of him.

In the first case, the caller of the instance creates the called instance, which inevitably requires that the called class appear in the caller's code. The loose coupling between the two can not be realized.

In the second case, the caller does not need to care about the specific implementation process of the callee, but only needs to find an instance that meets a certain standard (interface) and can be used. The code invoked at this point is interface-oriented programming, which decouples the caller from the callee, which is why the factory pattern is widely used. But the caller needs to locate the factory on its own, and the caller is coupled to a specific factory.

In the third case, the caller does not need to locate the factory, and when the program runs to the callee, the dependency injection container automatically provides the callee instance. In fact, both the caller and the callee are under the management of the dependency injection container, and the dependency between them is provided by the dependency injection container. As a result, the coupling between the caller and the callee is further reduced, which makes the application easier to maintain, which is what dependency injection is intended to achieve.

Implementing a lightweight dependency injection Container with php

First, let's create a class that looks like this:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report