Php 当需要许多新对象实例时,如何实现依赖项注入?
我正在尝试理解一些DI概念。可以很容易地按照每个依赖项转换一个实例,如本例所示 非直接投资Php 当需要许多新对象实例时,如何实现依赖项注入?,php,dependency-injection,Php,Dependency Injection,我正在尝试理解一些DI概念。可以很容易地按照每个依赖项转换一个实例,如本例所示 非直接投资 $my_cal = new MyCal(); class MyCal { public function __construct() { $this->date = new MyDate(); $this->label = new MyLabel(); } } $my_date = new MyDate(); $my_label = ne
$my_cal = new MyCal();
class MyCal {
public function __construct() {
$this->date = new MyDate();
$this->label = new MyLabel();
}
}
$my_date = new MyDate();
$my_label = new MyLabel();
$my_cal = new MyCal($my_date, $my_label);
class MyCal {
public function __construct(MyDate $date_class, MyLabel $label_class) {
$this->date = $date_class;
$this->label = $label_class;
}
}
$my_cal = new MyCal();
class MyCal {
public function __construct() {
$today = new MyDate(...);
$tomorrow = new MyDate(...);
$next_day = new MyDate(...);
$yesterday = new MyDate(...);
$another_day = new MyDate(...);
// ...
$label1 = new MyLabel(...);
$label2 = new MyLabel(...);
$label3 = new MyLabel(...);
$label4 = new MyLabel(...);
// ...
}
}
DI
$my_cal = new MyCal();
class MyCal {
public function __construct() {
$this->date = new MyDate();
$this->label = new MyLabel();
}
}
$my_date = new MyDate();
$my_label = new MyLabel();
$my_cal = new MyCal($my_date, $my_label);
class MyCal {
public function __construct(MyDate $date_class, MyLabel $label_class) {
$this->date = $date_class;
$this->label = $label_class;
}
}
$my_cal = new MyCal();
class MyCal {
public function __construct() {
$today = new MyDate(...);
$tomorrow = new MyDate(...);
$next_day = new MyDate(...);
$yesterday = new MyDate(...);
$another_day = new MyDate(...);
// ...
$label1 = new MyLabel(...);
$label2 = new MyLabel(...);
$label3 = new MyLabel(...);
$label4 = new MyLabel(...);
// ...
}
}
但是,一个具有许多实例调用(例如30个)的类如何转换呢
非直接投资
$my_cal = new MyCal();
class MyCal {
public function __construct() {
$this->date = new MyDate();
$this->label = new MyLabel();
}
}
$my_date = new MyDate();
$my_label = new MyLabel();
$my_cal = new MyCal($my_date, $my_label);
class MyCal {
public function __construct(MyDate $date_class, MyLabel $label_class) {
$this->date = $date_class;
$this->label = $label_class;
}
}
$my_cal = new MyCal();
class MyCal {
public function __construct() {
$today = new MyDate(...);
$tomorrow = new MyDate(...);
$next_day = new MyDate(...);
$yesterday = new MyDate(...);
$another_day = new MyDate(...);
// ...
$label1 = new MyLabel(...);
$label2 = new MyLabel(...);
$label3 = new MyLabel(...);
$label4 = new MyLabel(...);
// ...
}
}
这可能是使用容器或工厂的时候吗?解决方案非常简单。
您只需要传递一次依赖项。在这种情况下,您应该执行以下操作:
$date = new MyDate();
class MyCal {
function __construct( MyDate $dateService ) {
$today = $dateService->get('today');
$tomorrow = $dateService->get('tomorrow');
$next_day = $dateService->get('next_day');
...
}
}
这样,您就暴露了这样一个事实:您的类依赖于另一个
MyDate
对象,您只需传递它一次。这只提供了传入类的一个实例。在我的示例中,我使用了几个。您看到那些->get
方法了吗?如果我正确理解了这一点,MyDate类“get”方法将处理日期创建,而不是不同的日期工厂类?我想知道是否$today=$dateService->get('04/08/2013')代码>在您的示例中更有意义。基本上是使用工厂吗?我仍然有点困惑,因为您的示例没有使用单独的工厂类,比如您链接的wiki。我正在尝试实现一种干净的代码方法,比如,如果这有助于解释我在做什么。