1

I am very new to Magento 2. After reading through some of the classes, I just noticed that there are a lot of interfaces injected directly through constructions. We all know the interfaces are merely method signatures. How does Magento get the concrete method feature by just injecting an interface.

//vendor\magento\module-indexer\Model\Processor.php

public function __construct(
    ConfigInterface $config,
    IndexerInterfaceFactory $indexerFactory,
    Indexer\CollectionFactory $indexersFactory,
    \Magento\Framework\Mview\ProcessorInterface $mviewProcessor
) {
    $this->config = $config;
    $this->indexerFactory = $indexerFactory;
    $this->indexersFactory = $indexersFactory;
    $this->mviewProcessor = $mviewProcessor;
}

And it calls in this method

public function updateMview()
{
    $this->mviewProcessor->update('indexer');
}

How does the interface and it's implementation works?

3

See the link:

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/depend-inj.html

Magento 2 use oops concepts of PHP.

. As the interface is not able to provide a method (function) definition, So, at Magento, every interface is overridden by a PHP class.

Let me give an example, If you want to get an order data then you have to use Magento\Sales\Api\OrderRepositoryInterface::get()

If you check at this interface class you will find that only method signature is exiting that interface.

/**
 * Loads a specified order.
 *
 * @param int $id The order ID.
 * @return \Magento\Sales\Api\Data\OrderInterface Order interface.
 */
public function get($id);

That interface is overridden by PHP class \Magento\Sales\Model\OrderRepository where you find the function definition and its business logic.

This write is managed from di.xml.

<preference for="Magento\Sales\Api\OrderRepositoryInterface" type="Magento\Sales\Model\OrderRepository"/>

If you want to create an object of class at core php then you are write like

$object = new MyClass;

Adding an interface to __construct() means you are creating an object of a class/interface.

here public function __construct( ConfigInterface $config, You have created an object of class ConfigInterface which is $config. which basically create object of Magento\Indexer\Model\Config

So you can access all public methods of Config using $config .

As at di.xml 's Magento\Framework\Indexer\ConfigInterface override by Magento\Indexer\Model\Config, So basically you can access of all public Magento\Indexer\Model\Config

<preference for="Magento\Framework\Indexer\ConfigInterface" type="Magento\Indexer\Model\Config" />

Adding Interfaces/classes to __construct() method you are creating an object of that class/interface which required for your work.

And this is the concept of use interfaces at contract().

Also, interface only expose public method NOT your private and protected methods that make your code Core more secure.

4
  • Thanks for the explanation. One question what if I don't use the preference in di.xml, will it throw an error? Jun 7 '20 at 9:11
  • 1
    it will, you can't create an instance of a bare interface, this is common in most programming languages, the di.xml acts as a link between the interface and it's implementation, that's why you can use interface in the constructors Jun 7 '20 at 10:14
  • thx for clarification Jun 7 '20 at 10:51
  • Where does this actually happen to replace interface class to model class Mar 5 '21 at 7:17
2

The interface and implementation is called a service contract. The interfaces are binded to the model in di.xml

The idea is that you expose only the API, not the actual implementation which models are giving us.

Another thing is that the Interfaces in magento, can be used in WEBAPI. This is pretty awesome if you get along with it a bit ;)

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/service-contracts/service-contracts.html

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.