In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to achieve dependency injection in PHP, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
What is 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 this case, when the instance caller creates the called instance, it is necessary to require the called class to 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.
How to implement dependency injection in PHP?
Dependency injection has outstanding advantages in current programming, which is the reason why ThinkPHP, Laravel and other frameworks use this programming idea. So how exactly are these frameworks implemented using PHP?
Let's use a simple case to talk about the mystery.
First, let's create a class that looks like this, containing an attribute and two sides:
Suppose we now have two other classes, redisDB and cache,redisDB provide the operation of a redis database, and cache is responsible for the implementation of caching and relies on redisDB. As follows:
In the above class, we simply implement the query, save, and delete of redis. The other class is similar to the current class structure:
Now that we have implemented redisDB and cache, let's not discuss the details here, let's see how to use them. First, you need to inject two components into the container:
At this point, you may find it a little cumbersome. The structures of cache and redisDB are so similar that redis can be written into cache without having to be separated separately. But have you ever thought that some data are not so timely and the quantity is relatively large, it is a bit inappropriate to use redis, mongodb is a better choice; some data are updated more slowly, and there is no requirement for query speed, so it may be more appropriate to write files directly to the hard disk; or, your customer thinks that redis operation and maintenance is a little difficult and asks you to replace it with memcache.... That's why it was isolated. Then, continue to improve the code:
We have added a new interface BackendInterface, which stipulates that the three classes of redisDB,mongoDB,file must implement the functions required by this interface. As for other icing on the cake, you can play as much as you like. The code for cache doesn't seem to have changed, because cache doesn't need to care about how the data is stored in the database or file. On the other hand, the caller of cache does not need to care about how cache is implemented, just implement the corresponding method according to the interface. You will benefit more from multi-person cooperation. You just need to agree on the interface and implement it separately.
The above code can continue to be improved until you think it is impeccable. For example, a redis service may be called multiple times in a request, and each call will be recreated, which can degrade performance. Just extend the DI container to add a parameter or a method, whatever you want.
In this way, if a service has to be called multiple times in a single request, you can set the shared property to true to reduce unnecessary waste. If you find it a bit cumbersome to setDi every time you inject it, and want him to setDi automatically, you can do this:
Then, you can go like this:
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.