Note: If you specify the specific module in question, I can update my answer to be more specific.
Depending on the need, you can leverage service contracts for this functionality.
When you use dependency injection in the Magento 2 system, you can specify service contracts, (lower level interfaces) that serve as the basis for your class implementation.
You need to specify the low level service contract (interface) that implements the optional functionality. Once you have specified that interface, you (or Magento) can then specify the preference for that model through the di.xml
.
As an example, if you were trying to instantiate your class and pass the product object to your constructor, (which you shouldn't, but that's another article), you specify its low level interface.
Like this:
<?php
namespace Coolryan\Di\Model;
class Example extends \Magento\Framework\Model\AbstractModel {
protected $product;
public function __construct(
Magento\Catalog\Api\Data\ProductInterface $product
) {
$this->product = $product;
}
}
From there, you can specify the preference for your class being instantiated through your di.xml
, like so:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Api\Data\ProductInterface" type="Magento\Catalog\Model\Product" />
</config>
Now the $product
instance passed to the constructor will be the class Magento\Catalog\Model\Product
. The object manager knows to pass this class because of the preference you specified in your xml.
I hope this helps.