Magento 2 Virtual Type programatically

Creating VirtualType in Magento 2

The purpose to create the virtual type is to create the sub-class for the existing class. Like

<?php
class OurVirtualTypeName extends \VendorName\ModuleName\Model\Virtualtype
{
}

This code is inserted into the module's di.xml to create the virtualtype in Magento 2 

Directory

app/code/VendorName/ModuleName/etc/di.xml

Content for this file

<config>
    <!-- ... -->
    <virtualType name="ourVirtualTypeName" type="VendorName\ModuleName\Model\Virtualtype">  
    </virtualType>        
</config>

Namely, the nodes are placed under the main node and include two attributes: name and type. While the name attribute is mostly the universally special name of that node, the type attribute is the real PHP for the virtual type.

As you see, it is simple to give some descriptions about the virtual type. If you erase the cache and repeat the request, the output is still the same.

$ php bin/magento hw:tutorial-virtual-type
First, we'll report on the VendorName\ModuleName\Model\Example object
The Property $property_of_example_object
  is an object
  created with the class: 
  VendorName\ModuleName\Model\Virtualtype

Next, we're going to report on the Example object's one property (an Virtualtype class)
The Property $property_of_argument1_object
  is an object
  created with the class: 
  VendorName\ModuleName\Model\Argument2

Finally, we'll report on an Virtualtype object, instantiated separate from Example
The Property $property_of_argument1_object
  is an object
  created with the class: 
  VendorName\ModuleName\Model\Argument2

Using Virtual Type in Magento 2

Directory

app/code/VendorName/ModuleName/etc/di.xml

Content for this file

<config>
    <!-- ... -->
    <virtualType name="ourVirtualTypeName" type="VendorName\ModuleName\Model\Virtualtype">  
    </virtualType>        

    <type name="VendorName\ModuleName\Model\Example">
        <arguments>
            <argument name="the_object" xsi:type="object">Some\Other\Class</argument>
        </arguments>
    </type>        

</config>

However, applying that command will lead you to the following error even though you have already cleaned your cache.

$ php bin/magento hw:tutorial-virtual-type

  [ReflectionException]                  
  Class Some\Other\Class does not exist 

Let’s move to a smart way with the virtual type. This means Some\Other\Class is recovered by ourVirtualTypeName. You can ensure that no error is caused unless you call the command with the above in place.

#File: app/code/Mageplaza/HelloWorld/etc/di.xml      
<config>
    <!-- ... -->
    <virtualType name="ourVirtualTypeName" type="Mageplaza\HelloWorld\Model\Virtualtype">  
    </virtualType>        

    <type name="Mageplaza\HelloWorld\Model\Example">
        <arguments>
            <argument name="the_object" xsi:type="object">ourVirtualTypeName</argument>
        </arguments>
    </type>        

</config>

 

If you are looking for Magento Developers, visit Magento Web Agency.