Simple understanding of Inversion of Control (IOC, (Inversion of Control))
At first, our code dependencies may look like this, here are just examples There are cross-dependencies between three objects and three classes. The actual dependency relationship of our code is actually much more complicated. I can imagine the intersection of more than ten objects, and even horizontal intersection.
Now comes the problem , due to irresistible reasons, PHP version upgrade, requirement changes, etc., we need to replace the α object and delete the β object. How many code changes do we need to make?
It’s time to come up with the IOC .
Instantly refreshing.
The IOC container maintains a collection of object instances and class names globally. When we write a class, we The dependent objects are registered in the container, and are instantiated when the class is called.
This is the idea of IOC. A system achieves "inversion of control" by completely separating organizational control and objects. For Dependency injection, which means achieving decoupling by controlling and instantiating dependent objects elsewhere in the system.
Related recommendations:The latest five Laravel video tutorials
The above is the detailed content of What is IoC (Inversion of Control)? Two pictures to help you figure it out!. For more information, please follow other related articles on the PHP Chinese website!