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