Sunday, January 9, 2022

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

Issue

I have this error:

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

I'm working with the framework Yii and want to use XMLReader inside a component.

<?php 

namespace backend\components;

class XMLRead {

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

Solution

That is because you're using XMLReader class inside of backend\components namespace so XMLReader is interpreted as backend\components\XMLReader. You should either use leading backslash to indicate that class from global namespace should be used:

$xml = new \XMLReader();

Or import this class using use statement in head of your file:

<?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();
        } 
    } 
} 

You can read more about namespaces in documentation.



Answered By - rob006

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.