Let's say I'm building a Magento 2 extension that does....well...not important. Let's say it does super awesome stuff.
But I want to make sure that this is build using the proper standards so other developers can extend it.
When should I use the DI in combinations with interfaces and when I shouldn't?
To make it clear here is a core example.
The class Magento\Core\Helper\Data
has a constructor like this:
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;
}
My question is focused on the var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
(I know there are others in the same constructor, but one explanation will fit all cases I think).
According to the di.xml
from the core module the var will be an instance of Magento\Framework\App\Config
:
<preference for="Magento\Framework\App\Config\ScopeConfigInterface" type="Magento\Framework\App\Config" />
but I can easily change that if I need.
When should I use interfaces like that in my code?
I've created this incomplete sample module (sorry for advertising), where I've used such interfaces, but all of them come from the core. I haven't created one of my own. Should I?