Laravel依赖
Posted 叨叨斋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel依赖相关的知识,希望对你有一定的参考价值。
首先有个接口
interface Visit {
public function go ();
}
然后有三个类Leg,Car,Train
class Leg implements Visit {
public function go (){
echo "walt to 新疆";
}
}
class Car implements Visit {
public function go () {
echo "drive car to 新疆";
}
}
class Train implements Visit {
public function go () {
echo "go to 新疆 by train";
}
}
依赖关系:有一个旅行者Traveller
class Traveller {
protected $trafficTool;
public function __construct () {
//依赖产生
$this->trafficTool = new Leg(); //选择步行
}
public function visitXinjiang () {
$this->trafficTool->go(); //去新疆玩啦
}
}
$visit = new Traveller();
$visit->visitXinjiang();
简单工厂模式:
//工厂
class TracfficToolFactory{
public function createTracfficTool($name) {
switch ($name) {
case ‘Leg‘:
return new Leg();
break;
case ‘Car‘:
return new Car();
break;
case ‘Train‘:
return new Train();
break;
}
}
}
//Traveller
class Traveller {
protected $TrafficTool;
public function createTraffic ($name) {
$this->TrafficTool = new TracfficToolFactory()->createTracfficTool($name);
}
public function visitXinjiang () {
$this->TrafficTool->go();
}
}
$traffic = new Traveller();
$traffic->createTraffic(‘Leg‘);
$traffic->visitXingjiang();
以上是关于Laravel依赖的主要内容,如果未能解决你的问题,请参考以下文章