PHP的依赖注入
2018-10-18 本文已影响60人
安之烟波客
闲来无事,今天兴致来了 ,看了看依赖注入,写了以下一段代码,不知道算不算依赖注入,自我感觉挺方便的,但是又说不上来!欢迎大伙前来指点!谢谢
<?php
/**
* 基本接口
*/
interface noticeInterface{
public function send($content);
public function rec();
}
class mail implements noticeInterface{
public function send($content){
}
public function rec(){
}
}
class telsms extends mail{
public function send($content){
echo "用电话发短信告诉你:".$content.'<br/>';
}
}
class weichart extends mail{
public function send($content){
echo "用微信发语音告诉你:".$content.'<br/>';
}
}
class work{
private $worker;
public function __construct(mail $mail){
return $this->worker = $mail;
}
public function dowork(){
return $this->worker;
}
}
$obj = new work(new telsms());
$obj->dowork()->send("我是来打酱油的!");
$obj = new work(new weichar());
$obj->dowork()->send("我是来打酱油的!");
image.png