Home >Backend Development >PHP Tutorial >Why does php use dependency injection?
Because dependency injection will reduce the coupling between the dependency and the dependent type, when modifying the dependent type implementation, there is no need to modify the dependent type implementation; at the same time, it can be more convenient to use for dependent type testing "Mocking object" replaces the original dependent type to achieve the purpose of independent unit testing of dependent objects.
0. Introduction
In the field of software engineering, dependency injection (Dependency Injection) is used to implement control feedback. One of the most common ways of Inversion of Control. This article mainly introduces the principles and common implementation methods of dependency injection, focusing on the applicable scenarios and advantages of this young design pattern.
1. Why is dependency injection needed?
Inversion of control is used for decoupling. Who is decoupled from whom? This was the first question I had when I first learned about dependency injection.
Below I quote a part of the code used by Martin Flower when explaining injection to illustrate this problem.
public class MovieLister { private MovieFinder finder; public MovieLister() { finder = new MovieFinderImpl(); } public Movie[] moviesDirectedBy(String arg) { List allMovies = finder.findAll(); for (Iterator it = allMovies.iterator(); it.hasNext();) { Movie movie = (Movie) it.next(); if (!movie.getDirector().equals(arg)) it.remove(); } return (Movie[]) allMovies.toArray(new Movie[allMovies.size()]); } ... }
public interface MovieFinder { List findAll(); }
We created a class named MovieLister to provide the required movie list, and its moviesDirectedBy method provides a way to search for movies based on the director name. What is really responsible for searching for movies is MovieFinderImpl, which implements the MovieFinder interface. Our MovieLister class creates a MovieFinderImpl object in the constructor.
So far, everything seems good. However, when we want to modify the finder and replace the finder with a new implementation (such as adding a parameter to MovieFinder to indicate which database the Movie data comes from), we not only need to modify the MovieFinderImpl class, but also need to modify the MovieFinderImpl created in our MovieLister code.
This is the coupling that dependency injection deals with. This way of creating MovieFinderImpl in MovieLister makes MovieLister not only rely on the MovieFinder interface, it also relies on the implementation of MovieListImpl. This kind of code that directly creates objects of another class in one class, like hard-coded strings and hard-coded numbers (magic numbers), is a bad smell that leads to coupling. We can put this The bad smell is called hard init. At the same time, we should also remember like hard coding that new (object creation) is poisonous.
The main disadvantages brought by Hard Init are two aspects: 1) When modifying its implementation as mentioned above, you need to modify the code where it was created; 2) It is not easy to test, and the classes created in this way (above) MovieLister in this article) cannot be tested alone, and its behavior is tightly coupled with MovieFinderImpl. At the same time, it will also cause code readability problems ("If a piece of code is not easy to test, then it must not be easy to read.").
2. How to implement dependency injection
Dependency injection is actually not magical. Dependency injection is used in many of our daily codes, but we rarely notice it. , and rarely actively use dependency injection for decoupling. Here we briefly introduce the three methods of relying on injection.
2.1 Constructor Injection
This is the simplest dependency injection method in my opinion. Let’s modify the constructor of MovieList in the above code so that the implementation of MovieFinderImpl is in the MovieLister class Created outside. In this way, MovieLister only depends on the MovieFinder interface we defined, not on the implementation of MovieFinder.
public class MovieLister { private MovieFinder finder; public MovieLister(MovieFinder finder) { this.finder = finder; } ... }
2.2 Setter injection
Similarly, we can add a setter function to pass in the created MovieFinder object, which can also avoid hard init of this object in MovieFinder.
public class MovieLister { s... public void setFinder(MovieFinder finder) { this.finder = finder; } }
2.3 Interface injection
Interface injection uses the interface to provide the setter method, and its implementation is as follows.
First create an interface for injection.
public interface InjectFinder { void injectFinder(MovieFinder finder); }
After that, we let MovieLister implement this interface.
class MovieLister implements InjectFinder { ... public void injectFinder(MovieFinder finder) { this.finder = finder; } ... }
Finally, we need to create dependent MovieFinder implementations according to different frameworks.
3. Finally
Dependency injection reduces the coupling between dependencies and dependent types. When modifying the implementation of the dependent type, there is no need to modify the implementation of the dependent type. , at the same time, for dependent type testing, it is more convenient to use mocking object to replace the original dependent type, so as to achieve the purpose of independent unit testing of dependent objects.
Finally, it should be noted that dependency injection is just one way of implementing inversion of control. Another common implementation of inversion of control is called dependency lookup.
The above is the detailed content of Why does php use dependency injection?. For more information, please follow other related articles on the PHP Chinese website!