将 XmlReader 与 Yii 结合使用 - Class 'backend\components\XMLReader' 未找到

Using XmlReader with Yii - Class 'backend\components\XMLReader' not found

我有这个错误:

PHP Fatal Error – yii\base\ErrorException Class 'backend\components\XMLReader' not found.

我正在使用 Yii 框架并希望在组件中使用 XMLReader

<?php 

namespace backend\components;

class XMLRead {

    public function parse() {
        // Instanciation de la classe XMLReader
        try {
            $xml = new XMLReader();
        } catch (Exception $e) {
            $e->getMessage();
        } 
    } 
} 

您应该使用 XmlReader 进行考试

use omgdef\fias\console\base\XmlReader;

不知道你用的是哪个包。但这是有效的

https://github.com/OmgDef/yii2-fias

这里还有一些来自回购协议的例子

https://github.com/OmgDef/yii2-fias/blob/aa8980fb40a003697d873df3829b4a440923144f/console/controllers/FiasController.php

您应该使用项目中的命名空间

那是因为您在 backend\components 命名空间内使用 XMLReader class,所以 XMLReader 被解释为 backend\components\XMLReader。您应该使用前导反斜杠来指示应该使用全局命名空间中的 class:

$xml = new \XMLReader();

或在文件头部使用 use 语句导入此 class:

<?php 

namespace backend\components;

use XMLReader;

class XMLRead {

    public function parse() {
        // Instanciation de la classe XMLReader
        try {
            $xml = new XMLReader();
        } catch (Exception $e) {
            $e->getMessage();
        } 
    } 
} 

您可以在 documentation 中阅读有关名称空间的更多信息。