Brief description IOC
<?PHP//IOC ———— design mode//control reversal inversion of control//dependency transfer//dependency abstraction rather than practice//used to solve the problem of high-level applications relying on the underlying components, poor software portability (modification difficulties)//instances:/*class soft{Private $writer; Public Function __construct () {$this->writer = new Floppywriter; Public Function Save () {$this->writer->savetofloppy (); }}//caused high-level dependence on the bottom of the program can not be reused bad design class floppywriter{public Function Savetofloppy () {echo __method__; }} $soft = new soft; $soft->save ()*/######################### defining an interface contract #改进 ##########################//Interfaceidevicewriter{ Public functionsavetodevice ();}classsoft{Private $_writer; Public functionSetwriter ($name){ $this->_writer =$name; } Public functionSave () {$this->_writer->Savetodevice (); }}//Floppy disk storageclassFoppywriterImplementsidevicewriter{ Public functionSavetodevice () {Echo __method__; }}//USB StorageclassUsbwriterImplementsidevicewriter{ Public functionSavetodevice () {Echo __method__; }}//transfer high-level dependencies to "dependency injection"$soft=Newsoft ();$writer=NewUsbwriter;$soft->setwriter ($writer);$soft->save ();
PHP Simple Description IOC (PHP instance + comment)