I am using this config in di.xml
<virtualType name="ourFirstVirtualType" type="Test\Testmod\Model\Newmodelclass">
<arguments>
<argument name="test_object" xsi:type="object">Test\Testmod\Model\Example2</argument>
</arguments>
</virtualType>
<type name="Test\Testmod\Model\Modelclass">
<arguments>
<argument name="the_object" xsi:type="object">ourFirstVirtualType</argument>
</arguments>
</type>
This is the Modelclass
<?php
namespace Test\Testmod\Model;
class Modelclass
{
public $testProperty;
public function __construct(Newmodelclass $the_object)
{
$this->testProperty = $the_object;
}
}
and this is Newmodelclass
<?php
namespace Test\Testmod\Model;
class Newmodelclass
{
public $newTestProperty;
public function __construct(Againnewmodelclass $test_object)
{
$this->newTestProperty = $test_object;
}
}
Now I am trying to test virtual type with this controller
<?php
namespace Test\Testmod\Controller\Index;
use Test\Testmod\Model\Modelclass;
use Test\Testmod\Model\Newmodelclass;
use Test\Testmod\Model\Againnewmodelclass;
class Index extends \Magento\Framework\App\Action\Action
{
public function __construct(
\Magento\Framework\App\Action\Context $context,
Modelclass $Modelclass,
Newmodelclass $Newmodelclass,
Againnewmodelclass $Againnewmodelclass
) {
parent::__construct($context);
$this->Modelclass = $Modelclass;
$this->Newmodelclass = $Newmodelclass;
$this->Againnewmodelclass = $Againnewmodelclass;
}
public function execute()
{
echo "<pre>";
var_dump($this->Modelclass);
var_dump($this->Newmodelclass);
var_dump($this->Againnewmodelclass);
die;
}
}
I think this should work correct right but giving this error instead
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Test\Testmod\Model\Newmodelclass::__construct() must be an instance of Test\Testmod\Model\Againnewmodelclass, instance of Test\Testmod\Model\Example2 given, called in /var/www/html/magento226/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 111 and defined in /var/www/html/magento226/app/code/Test/Testmod/Model/Newmodelclass.php:7