php 如何重构 trait 里面的方法
2020年3月16日第一步 给需要重构的方法取一个 新名
1 2 3 4 |
use Notifiable { #给Notifiable trait 里面的 notify 方法定义个别名 为laravelNotify notify as protected laravelNotify; } |
第二步 使用旧名称构建新方法,并在方法内调用新名称方法达到重构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * 重写notify里面的方法 * * @param $instance */ public function notify($instance) { #如果要通知的人是当前用户,就不必通知了! if ($this->id == \Auth::id()) { return; } #只有数据库类型通知才需提醒,直接发送 Email 或者其他的都 Pass if (method_exists($instance, 'toDatabase')) { #$this->increment('notification_count'); } $this->laravelNotify($instance); } |