Magento 2 DI最佳做法
假设我正在构建一个Magento 2扩展程序,它确实......不重要。假设它的确很棒。 但是我想确保它是使用正确的标准构建的,以便其他开发人员可以对其进行扩展。 什么时候应该将DI与接口结合使用,什么时候不应该? 这里要说明的是一个核心示例。 该类Magento\Core\Helper\Data具有如下构造函数: public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\State $appState, PriceCurrencyInterface $priceCurrency, $dbCompatibleMode = true ) { parent::__construct($context); $this->_scopeConfig = $scopeConfig; $this->_storeManager = $storeManager; $this->_appState = $appState; $this->_dbCompatibleMode = $dbCompatibleMode; $this->_priceCurrency = $priceCurrency; } 我的问题集中在var上\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig(我知道同一构造函数中还有其他变量,但是一种解释适合我认为的所有情况)。 根据di.xml核心模块中的,var将是以下实例Magento\Framework\App\Config: <preference for="Magento\Framework\App\Config\ScopeConfigInterface" type="Magento\Framework\App\Config" /> 但是我可以根据需要轻松更改它。 什么时候应该在代码中使用类似的接口? …