class SendingService {
private $sendService;
public function __construct(Sending $smsService) {
$this->sendService = $smsService;
}
public function send($message) {
return $this->sendService->send($message);
}
}
interface Sending {
public function send($message);
}
class SmsService implements Sending {}
class EmailService implements Sending {}
最简单的例子就是一个路由,当你的controller方法拦截路由中的id并转换为需要的类
用户控制器.php
总的来说,现在Laravel中无处不在:Events、Listeners等常用的类中,这无非是通过构造函数或setter将一个类引入另一个类
例子: