PHP Dependency Injection

Source: Internet
Author: User

For dependency Injection My understanding now is to put a method as a variable into the formal parameter of another method.

<?phpclass Factory {public    static function Getdb () {        return new Db ();}    } Class Db {public    static function query ($sql) {        echo $sql;    }    public static function Querys ($sql) {        echo $sql. S ";    }} Class Example {    private $_db;    function GetList () {        $this->_db->query ("123");//Here the specific SQL statement is omitted to write    }    //external injection of the DB connection    function Setdb ($connection) {        $this->_db = $connection;    }} Call $example = new Example (), $example->setdb (Factory::getdb ());//Inject DB connection $example->getlist ();

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------

Simple point

<?phpclass Email {public    static function SendEmail () {        echo "Send Mail";    }} Class SMS {public    static function sendsms () {        echo "send SMS";    }} Class Example {    private $_send;    function Setsend ($send _function) {        $this->_send = $send _function;    }} $example = new Example (); $example->setsend (Sms::sendsms ());

Example like a registered class, after the successful registration, you want to send a text message

$example = new Example (); $example->setsend (Sms::sendsms ());

If you want to send mail,

$example = new Example (); $example->setsend (Email::sendemail ());

PHP 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.