Proxies(代理)
Magento 2的 构造函数注入模式 使您能够灵活地管理类依赖项。
作为一个例子,考虑以下两个类:
class SlowLoading
{
public function __construct()
{
// ... Do something resource intensive
}
public function getValue()
{
return 'SlowLoading value';
}
}
class FastLoading
{
protected $slowLoading;
public function __construct(
SlowLoading $slowLoading
){
$this->slowLoading = slowLoading;
}
public function getFastValue()
{
return 'FastLoading value';
}
public function getSlowValue()
{
return $this->slowLoading->getValue();
}
}
<type name="FastLoading">
<arguments>
<argument name="slowLoading" xsi:type="object">SlowLoading\Proxy</argument>
</arguments>
</type>