18

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?

  • "Let's say it does super awesome stuff." Does it have a floating heads of people that are online in stackexchange? Cause I would install that ;) – David Manners Aug 22 '14 at 6:59
  • 1
    @DavidManners I will try to port all my 1.x extensions to 2.0 so that will be ported also. I don't know about the floating part, but I'll see what I can do. – Marius Aug 22 '14 at 7:02
  • Imho that's not a Mage2 specific question, more generally "When to use interfaces". That depends where you want someone to be able to extend your extension. I would say everywhere where you work with business logic, which might change. ;) Besides that objects without behavior (e.g. simple data objects) usually won't change. – Tobias Aug 22 '14 at 7:38
  • 1
    @TobiasZander, you mean to say that I have to create an interface for almost EVERYTHING? Damn that's a lot of work. – Marius Aug 22 '14 at 7:40
  • @Marius, if you want to be 100% flexible, somehow yes. But I would not over-engineer it. I personally also like odetocode.com/blogs/scott/archive/2009/06/08/… as an introduction when it's actually useful – Tobias Aug 22 '14 at 7:49
9

Imho that's not a Mage2 specific question, more generally "When to use interfaces". That depends where you want someone to be able to extend your extension. I would say everywhere where you work with business logic, which might change. ;) Besides that objects without behavior (e.g. simple data objects) usually won't change.

If you want to be 100% flexible, somehow you have to use interfaces everywhere. But I would not over-engineer it. I personally also like http://odetocode.com/blogs/scott/archive/2009/06/08/when-do-i-use-interfaces.aspx as an introduction when it's actually useful.

7

Magento2 promote the usage of SOLID principles.

Dependency inversion principle direct says, that code should depend on abstractions (interfaces).

Interface segregation principle says that many client-specific interfaces are better than one general-purpose interface. Classes also can define protected interface, so the interfaces more preferred from an architectural point of view.

Also PHP do not support multiple inheritance of classes, but support multiple implementation of interfaces. That is one more point for interfaces.

So, a simple rule can be used: if you do not know what to use, ALWAYS use interfaces.

PS. Performance it's not reason for me to not use interfaces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.