mardi 19 janvier 2016

Magento 2 difference between Object Argument replacement and Class Preference

Take the following code as example:

// Vendor\Namespace\Model\SomeClass
// ...
public function __construct(
    ExampleClass $object   
{
    $this->object = $object;
}
// ...

I can use argument replacement to configure the object manager to use another class that inherits from ExampleClass in di.xml:

<config xmlns:xsi="http://ift.tt/ra1lAU" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <!-- ... -->
    <type name="Vendor\Namespace\Model\SomeClass">
        <arguments>
            <argument name="object" xsi:type="object">Vendor\Namespace\Model\AnotherExample</argument>
        </arguments>
    </type>
</config>

I can also configure a preference to replace the class:

<!-- .... -->
<preference
    for="Vendor\Namespace\Model\Example"
    type="Vendor\Namespace\Model\AnotherExample" />
<!-- ... -->

What is the difference between these two methods?





Aucun commentaire:

Enregistrer un commentaire