Can someone please explain when you would use <type/>
or <virtualType
in your di.xml
file?
I want to make sure I understand it correctly..
At the moment, this is what I sort of think the difference is, especially with xsi:type='object'
as arguments.
Type:
<type name='Some\File\Object'>
<arguments>
<argument name="someText" xsi:type="string">String Value for This Arg</argument>
<argument name="someArg" xsi:type="object">Some\Object\Arg\For\This\Arg</argument>
</arguments>
</type>
Virtual Type:
<virtualType name='instance1' type='Some\File\Object'>
<arguments>
<argument name="someArg" xsi:type="object">Some\Object\Arg\For\This\Arg</argument>
</arguments>
</virtualType>
<virtualType name='instance2' type='Some\File\Object'>
<arguments>
<argument name="someArg" xsi:type="object">Some\Other\Object\Arg\For\This\Arg</argument>
</arguments>
</virtualType>
So the questions I guess: - This is what I think it does:
<type/> vs </virtualType>
usage:<type/>
: This is a singular instance, and everywhere Magento 2 references this type, would it replace the argument given for every other class that use this object (argument)?<virtualType/>
: These ones you are able to call byname=
, (in my example instance1, or instance2) to differentiate their usage at different places. I then call them and give them different argument values, so they do not interact with each other?
Is this how you would use
type
andvirtualType
.... one/more objects and then 'override' them for use in other places that do not have any dependency on each other. For example: creating different log files, Magento has the mainxsi:type=object
referenced somewhere, then using virtualTypes I call them using thename
?Do I do this (based on
instance1
orinstance2
above) example usage:
Use Type
<type name='Some\Other\ObjectB'>
<arguments>
<argument name="ObjectInstance" xsi:type='object'>instance1</argument>
</arguments>
</type>
<type name='Some\Other\ObjectB'>
<arguments>
<argument name="ObjectInstance" xsi:type='object'>instance2</argument>
</arguments>
</type>
OR VirtualType:
<virtualType name='Some\Other\ObjectB'>
<arguments>
<argument name="ObjectInstance" xsi:type='object'>instance1</argument>
</arguments>
</virtualType>
<virtualType name='Some\Other\ObjectB'>
<arguments>
<argument name="ObjectInstance" xsi:type='object'>instance2</argument>
</arguments>
</virtualType>
I think the confusion is when do I use <virtualType/>
elements do I use them in conjunction with AND / OR , especially when I want to use multiple virtualTypes with just arguments different (log files for sake of this discussion)