The idea of IOC is widely used in Java development and is also widely used in PHP.
1 Interface Coder 2 {3 Public function coding (); 4 }
Implementing Class Javaer
1 classJavaerImplementsCoder2 {3 var $name;4 5 Public function__construct ($name)6 {7 $this->name =$name;8 }9 Public functionCoding ()Ten { One Echo"$this->name.is Coding Java code.<br> "; A } -}
Implementing Class Phper
1 classPhperImplementsCoder2 {3 var $name;4 5 Public function__construct ($name)6 {7 $this->name =$name;8 }9 Public functionCoding ()Ten { One Echo"$this->name.is Coding php code.<br> "; A } -}
Business Logic Class Task
1 classTask2 {3 var $name;4 var $owner;5 6 Public function__construct ($name)7 {8 $this->name =$name;9 }Ten One Public functionSetOwner ($owner){ A $this->owner =$owner; - } - the Public functionStart () - { - Echo"$this->name.started!<br> "; - $this->owner->coding (); + } -}
Test
1 $task New Task ("Task #1"); 2 // $owner = new Phper ("XIAOHB"); 3 $owner New Javaer ("Xiaoxiao"); 4 $task->setowner ($owner); 5 $task->start ();
The maintainability and extensibility of the code is obvious.
Dependency Injection (DI) and control inversion (IOC) "Review"