program = algorithm + data structure
The data structure restricts the ===>>>> dependency injection of the algorithm
Dependency injection is the idea of solving data structure and algorithm coupling
<?PHP/** * Created by Phpstorm. * DATE:2016/5/25 * time:18:09 * Container class Dependency Injection*/namespace Frontend\controllers; UseYii; UseYii\web\controller; UseYii\di\container;//Container class Dependency Injection classDependencyinjectcontrollerextendscontroller{ Public functionActionindex () {$container=NewContainer (); $container->set (' Frontend\controllers\driver ', ' frontend\controllers\mandriver '); //The get method instantiates the Mandriver first, and then passes the return value to car $car=$container->get (' Frontend\controllers\car '); $car-run (); } }Interfacedriver{ Public functionDrive ();} classMandriverImplementsdriver{ Public functionDrive () {Echo"I am an old man! "; } } classcar{Private $driver=NULL; //Public function __construct (mandriver $driver)//mandriver Strong Association, coupling is too high, so use interface Public function__construct (Driver$driver)//Line 20th implements interface delivery, eliminating strong dependencies { $this->driver =$driver; } Public functionrun () {$this->driver->Drive (); } }
Yii Container class Dependency Injection