Yii Container class Dependency Injection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.