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

What does php dependency injection mean?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what does php dependency injection refer to". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In PHP, dependency injection means that dependencies on classes are automatically injected through the constructor. In the controller architecture method and operation method, once the parameters are constrained by the object type, the dependency injection will be divided automatically. Because the parameters of the access controller come from the URL request, the ordinary variables are obtained automatically through the parameter binding, and the object variables are generated by dependency injection.

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.

What does php dependency injection mean?

Dependency injection essentially refers to the automatic injection of dependencies on the class through the constructor. For example, once the parameters are constrained by the object type in the controller architecture method and operation method, the dependency injection will be automatically triggered. Because the parameters of the access controller come from the URL request, the ordinary variables are automatically obtained through the parameter binding, and the object variables are generated through dependency injection.

The so-called dependency, for example, one class Person, another class Car, if a method of Person, such as drive, needs to reference Car, then it is said that the Person class depends on the Car class, extending to the object, this dependency is still true, for example, the object boy of the Person class depends on the object toyota of the Car class. Let's talk about the implementation of this drive method, assuming that the code is as follows:

Public Person {... public void drive () {Car toyota=new Car ("TOYOTA"); toyota. Put on file; toyota. Step on the accelerator; toyota. Hit the direction;}}

This dependency causes the object boy to be responsible for the creation of the object toyota or even the management of the entire life cycle, which obviously brings shortcomings such as high coupling and difficult maintenance. For example, to let the boy drive an Audi, you also need to modify the code of the class Person.

Therefore, a very famous principle is put forward in java's design theory, the principle of dependency inversion (Dependence Inversion). Its core idea is to transform the dependency between concrete classes into abstract dependency as far as possible, that is to say, class Person should rely on abstract class ICar, not concrete class Car. Here java strongly recommends the use of abstract and interface, as for the difference between abstract and interface. There is an introduction to any JAVA book, so I won't talk about it here.

This principle of relying on inversion is also reflected in a lot of design patterns, such as factory pattern and construction pattern. Personally, I think that control reversal IoC can also be regarded as a design pattern to achieve this principle. Control reversal, in which the word "control" has always been difficult to understand, but another term for control inversion is dependency injection (dependence injection), which I find easier to understand. Take boy and toyota as examples, the core of which is to inject toyota, an object that boy depends on, into boy without the need for boy to reference toyota itself. This injection process is usually completed by a control program without object concern, as an example:

Public Person {private ICar car;public Person (ICar onecar) {car=onecar;} public void drive () {car. Put on file; car. Step on the accelerator; car. Hit the direction;}}

At this point, the process of injecting and invoking is very simple, as follows:

Toyota toyota=new Toyota (); Person boy=new Person (toyota); boy.drive ()

Note: here we assume that the Toyota class is a concrete implementation of the ICar interface class.

This example demonstrates one of the simplest forms of injection, that is, constructor injection, which is achieved by injecting dependent objects into the object's constructor. Another commonly used method of injection is attribute injection, which means that it is achieved by injecting dependent objects into the properties of the object, or illustrated by the examples of boy and toyota, as follows:

Public Person {private ICar car;public Person () {} public void drive () {car. Put on file; car. Step on the accelerator; car. Direction;} public ICar getCar () {return this.car;} public void setCar (ICar onecar) {car=onecar;}}

At this point, the procedure for injection and invocation becomes as follows:

Toyota toyota=new Toyota (); Person boy=new Person (); boy.setCar (toyota); boy.drive ()

At this point, the concept of dependency injection should be relatively clear. Let's take a look at how to implement IoC in Spring, and see how Spring is a mature IoC container. In fact, IoC is implemented mainly through two concepts in Spring. First, objects and dependent objects are configured into a XML file through the XML configuration file. Of course, the XML file needs to comply with the specification specified by Spring, and then through the BeanFactroy class in the architecture. To automate the injection process described above, take boy and toyota as an example, as follows:

First of all, the Person class is the same.

Then add something to the xml configuration file-(assuming bean.xml):

Finally, the procedure to be called will be as follows:

That's all for BeanFactory factory=new XmlBeanFactory ("bean.xml"); Person boy= (Person) factory.getBean ("onePerson"); boy.drive (); "what does php dependency injection mean?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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