0

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://www.w3.org/2001/XMLSchema-instance" 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?

1 Answer 1

2

Shortly after writing this question I also figured out the solution:

The argument replacement will only replace the ExampleClass $object in the constructor of the type defined in di.xml, in this case Vendor\Namespace\Model\Someclass.

A configured class preference will inject an object of the defined type in every constructor of any class.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.