//依赖注入 Dependency_injection
class Calculator
{
public function calculate()
{
return '计算结果';
}
}
$calculator = new Calculator();
//构造方法注入
class Person
{
protected $calculator = null;
public function __construct(Calculator $calculator)
{
$this->calculator = $calculator;
}
public function operate()
{
return $this->calculator->calculate();
}
}
$person1 = new Person($calculator);
echo $person1->operate();
//操作方法注入
class People
{
public function operating(Calculator $calculator)
{
return $calculator->calculate();
}
}
$people = new People();
echo $people->operating($calculator);