# 使用 Magento 2 虚拟类型

**URL:** https://forum.magentochina.org/t/%E4%BD%BF%E7%94%A8-magento-2-%E8%99%9A%E6%8B%9F%E7%B1%BB%E5%9E%8B/840
**Category:** Magento 2.X
**Tags:** 开发, 教程, magento2
**Created:** [2020 年9 月 27 日 10:18 UTC](https://forum.magentochina.org/t/%E4%BD%BF%E7%94%A8-magento-2-%E8%99%9A%E6%8B%9F%E7%B1%BB%E5%9E%8B/840 "2020-09-27T10:18:50Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### 作者： ![Vicky](https://forum.magentochina.org/letter_avatar_proxy/v4/letter/v/b19c9b/32.png) [@Vicky](https://forum.magentochina.org/u/Vicky)
#### 发布日期： [2020 年9 月 27 日 10:18 UTC](https://forum.magentochina.org/t/%E4%BD%BF%E7%94%A8-magento-2-%E8%99%9A%E6%8B%9F%E7%B1%BB%E5%9E%8B/840/1 "2020-09-27T10:18:50Z")

</div>

当你运行基于 Magento 2 平台的虚拟商店时，使用虚拟类型是非常有必要的。在 Magento 2 平台中，di.xml 支持两种节点的类型，分别是 type 和 virtualtype ，同时，virtualtype被认为是代替type的一种理想方法。virtual tape 允许向已经存在的类中插入不同的依赖，而对其它类做出修改，根据本篇教程，Mageplaza 团队会引导你学会如何在 Magento 2 中创建和使用虚拟类型。

### 在 Magento 2 中创建虚拟类型（VirtualType）

在 Magento 2 中创建虚拟类型就是给已有的类创建一个子类，以下内容可以帮助您在 Magento 2 中创建虚拟类型。

```auto
<?php
class OurVirtualTypeName extends \Mageplaza\HelloWorld\Model\Virtualtype
{
}

```

将以下脚本代码插入到模块的 di.xml 中，用来在 Magento 2 中创建一个虚拟类型。

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

```

也就是说，这个节点被放在主节点下，同时包含了两个属性：name 和 type。虽然 name 属性是这个节点的主要特殊的通用名称，但是，虚拟类型的type 属性是真实的 PHP。

如你所见，给出虚拟类型的一些描述是非常简单的。如果您清除缓存并重复请求，则输出结果仍然是相同的。

```auto
$ php bin/magento hw:tutorial-virtual-type
First, we'll report on the Mageplaza\HelloWorld\Model\Example object
The Property $property_of_example_object
  is an object
  created with the class: 
  Mageplaza\HelloWorld\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: 
  Mageplaza\HelloWorld\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: 
  Mageplaza\HelloWorld\Model\Argument2

```

### 在 Magento 2 中使用虚拟类型

虚拟类型的功能取代了 PHP 类的位置，如果你要将自定义参数注入到 Example 类的构造函数中，您就没有必要使用以下配置了。

```auto
#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">Some\Other\Class</argument>
        </arguments>
    </type>        

</config>

```

但是，即使已经清除了缓存，应用该命令也会导致以下错误。

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

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

```

让我们用一种更聪明的方式使用虚拟类型。这意味着 Some\Other\Class 可以被 ourVirtualTypeName 恢复。您可以确保不会导致任何错误，除非您使用上述命令调用命令。

```auto
#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>

```

## 本教程翻译自Mageplaza模块开发系列，其他内容请点击下面链接:

> [@Magento2模块开发中文教程索引](https://forum.magentochina.org/t/magento2%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E4%B8%AD%E6%96%87%E6%95%99%E7%A8%8B%E7%B4%A2%E5%BC%95/36):
>
> 本文翻译自mageplaza的Magento2模块开发教程,而且正在翻译中. 翻译也是非常好的学习方式，希望下列表中还有我们没有翻译，而你又想分享的内容,请发表文章之后，在下面附上地址。 如果觉得本站翻译得内容对您有帮助，又没有时间帮助翻译回馈社区.请打赏我们，您的支持是我们的动力: 转载说明:非常欢迎转载，但是必须保证文章的完整性并附上原文地址和翻译原文的地址，保证阅读者不会产生歧义. 目录索引: 索 引 中 文 原 文 1 [Magento2如何创建模块](https://forum.magentochina.org/t/Magento-2%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA%E6%A8%A1%E5%9D%97%E6%95%99%E7%A8%8B/38)[How to Create Module in Magento 2](https://www.mageplaza.com/kb/how-create-hello-world-module-magento-2.html) 2 [控制器详解](https://forum.magentochina.org/t/Magento-2%E6%8E%A7%E5%88%B6%E5%99%A8%E8%AF%A6%E8%A7%A3%E6%95%99%E7%A8%8B/39)[Create Controllers](https://www.mageplaza.com/magento-2-module-development/how-to-create-controllers-magento-2.html) 3 [CRUD 模型](https://forum.magentochina.org/t/Magento2-CRUD%E6%A8%A1%E5%9E%8B%E8%B5%84%E6%BA%90%E9%9B%86%E5%90%88%E5%B7%A5%E5%8E%82%E5%AF%B9%E8%B1%A1%E8%AF%A6%E8%A7%A3%E6%95%99%E7%A8%8B/43)[CRUD Models](https://www.mageplaza.com/magento-2-module-development/how-to-create-crud-model-magento-2.html) 4 [视图层:块,布局和模板](https://forum.magentochina.org/t/%E8%A7%86%E5%9B%BE%E5%B1%82%E5%9D%97%E5%B8%83%E5%B1%80%E5%92%8C%E6%A8%A1%E6%9D%BF%E6%A8%A1%E6%9D%BF%E6%95%99%E7%A8%8B/50/1)[View: Block, Layouts and templates](https://www.mageplaza.com/magento-2-module-development/view-block-layout-template-magento-2.html) 5 [配置system.xml](https://forum.magentochina.org/t/%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA%E7%B3%BB%E7%BB%9F%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6systemxml/55)[Configuration - System.xm…](https://www.mageplaza.com/magento-2-module-development/create-system-xml-configuration-magento-2.html)
