Magento 2 - Type vs VirtualType

I wrote these tutorials for myself in future when I forget for the next steps.
Search for a command to run...

I wrote these tutorials for myself in future when I forget for the next steps.
No comments yet. Be the first to comment.
When you provision an Ubuntu instance on Oracle Cloud, you'll notice something different from standard Ubuntu installations: the system uses iptables instead of ufw for firewall management. This disti

When developing web applications locally, HTTPS is often overlooked. Yet, modern browsers and APIs increasingly require secure connections—even in development. That’s where mkcert comes in: a simple t

Testing the checkout success page in Magento 2 can be frustrating. By default, once an order is placed, Magento clears the quote session, meaning you can’t simply refresh or revisit the success page without going through the entire checkout process a...

Magento Commerce (EE) offers powerful features, but for many teams, Open Source (CE) is leaner, lighter, and more sustainable. If you're migrating from EE to CE, the Opengento downgrade tool provides a clean, scriptable way to remove proprietary modu...

If you’ve ever checked your Apache error logs and seen something like this: Code [core:error] (13)Permission denied: access to /robots.txt denied (filesystem path '/home/username/sites') because search permissions are missing on a component of the pa...

What is the differences between type and virtualType? in this article, I assume you already understand about the preferences & type. In this article, I tried to create custom command line of fiko:training
I defined helper file of app/code/Fiko/Training/Helper/Data.php (scroll down to see the file).
app/code/Fiko/Training/Console/Command/Testing.php defined for the custom command class.
app/code/Fiko/Training/etc/di.xml to define custom command and define type & virtualType
The basic difference is: type will affect all instances, whereas virtualType affects only on sub-class (specific instances)
if you use type to change class attribute, it will be used by all instances. All instances / classes use that class (app/code/Fiko/Training/Helper/Data.php) will have this value.
<?php
declare(strict_types=1);
namespace Fiko\Training\Helper;
class Data
{
/** @var array */
public $data;
/**
* Constructor
*
* @param array $data
*/
public function __construct(array $data = [])
{
$this->data = $data;
}
/**
* This method will return what's the bypassed parameter
*
* @param string $name
* @return string
*/
public function getName($name): string
{
return "{$this->data['prefix']} {$name}";
}
}
Notes:
<?php
declare(strict_types=1);
namespace Fiko\Training\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Fiko\Training\Helper\Data as HelperData;
class Testing extends Command
{
private $helper;
public function __construct(HelperData $helper)
{
$this->helper = $helper;
parent::__construct(null);
}
protected function configure(): void
{
$this->setName('fiko:training');
parent::configure();
}
/**
* Execute the command
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln($this->helper->getName('Borizqy'));
return 1;
}
}
Notes:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- BEGIN: Defining custom command -->
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="fiko:training" xsi:type="object">Fiko\Training\Console\Command\Testing</item>
</argument>
</arguments>
</type>
<!-- END: Defining custom command -->
<!-- BEGIN: example of type implementation -->
<type name="Fiko\Training\Helper\Data">
<arguments>
<argument name="data" xsi:type="array">
<item name="prefix" xsi:type="string">Mr.</item>
</argument>
</arguments>
</type>
<!-- END: example of type implementation -->
<!-- BEGIN: example of virtualType implementation -->
<type name="Fiko\Training\Console\Command\Testing">
<arguments>
<argument name="helper" xsi:type="object">data_example</argument>
</arguments>
</type>
<virtualType name="data_example" type="Fiko\Training\Helper\Data">
<arguments>
<argument name="data" xsi:type="array">
<item name="prefix" xsi:type="string">Tuan</item>
</argument>
</arguments>
</virtualType>
<!-- END: example of virtualType implementation -->
</config>
Notes:
Defining custom command, to define the custom command line of bin/magento fiko:training
example of type implementation, all instances use Fiko\Training\Helper\Data will have $data['prefix'] and the value is Mr..
example of virtualType implementation, specifically Fiko\Training\Helper\Data used by Fiko\Training\Console\Command\Testing will have have $data['prefix'] and the value is Tuan.
References: