依赖注入约束
<?php
/*依赖注入约束注入的类必需是实现visit接口的类*/
interface visit{
public function go();
}
class car implements visit{
public function go(){
echo '轮子跑';
}
}
class train{
public function go(){
echo '动车组';
}
}
class trive{
public $visit;
function __construct(visit $visit){//visit 接口的实现类
$this->visit = $visit;
}
public function triveType(){
$this->visit->go();
}
}
$car = new car();
/*$trive = new trive($car);
$trive->triveType();*/
$train = new train();
$trive = new trive($train);
$trive->triveType();//报错 因为注入的train实例没有实现visit接口
posted on 2018-03-18 19:01 running-fly 阅读(227) 评论(0) 编辑 收藏 举报