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.