Home  >  Article  >  PHP Framework  >  What is IoC (Inversion of Control)? Two pictures to help you figure it out!

What is IoC (Inversion of Control)? Two pictures to help you figure it out!

藏色散人
藏色散人forward
2021-11-17 14:22:591699browse

The dependencies of component dependencies are provided by constructing injection, function calls or attribute settings. It's that simple.

Simple understanding of Inversion of Control (IOC, (Inversion of Control))

What is IoC (Inversion of Control)? Two pictures to help you figure it out!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 .

What is IoC (Inversion of Control)? Two pictures to help you figure it out!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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete