<?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接口