Configuring class preferences
A great number of Magento's core classes pass interfaces around constructors. The benefit of this is that the object manager, with the help of di.xml, can decide which class to actually instantiate for a given interface.
Let's imagine the Foggyline\Di\Console\Command\DiTestCommand class with a constructor, as follows:
public function __construct(
\Foggyline\Di\Model\TestInterface $myArg1,
$myArg2,
$name = null
)
{
//Method body here...
}Note how $myArg1 is type hinted as the \Foggyline\Di\Model\TestInterface interface. The object manager knows that it needs to look into the entire di.xml for possible preference definitions.
We can define preference within the module's di.xml file, as follows:
<preference
for="Foggyline\Di\Model\TestInterface"
type="Foggyline\Di\Model\Cart"/>Here, we are basically saying that when someone asks for an instance of Foggyline\Di\Model\TestInterface, give it an instance of the Foggyline\Di\Model\Cart...