在使用命名空间时找不到SimpleXMLElement [英] SimpleXMLElement cannot be found when working with namespaces

查看:296
本文介绍了在使用命名空间时找不到SimpleXMLElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Wordpress中创建一个Widget,我遇到了创建一个SimpleXMLElement对象的问题。

I'm trying to create a Widget in Wordpress, and I'm running into an issue creating a SimpleXMLElement object.

这里是代码:

namespace GenieKnows_Search;  

class GenieKnows_Search_Widget extends \WP_Widget {
     //Constructor
     function __construct() {
         parent::__construct('genieknows_search_widget', 'GenieKnows_Search_Widget');
     }

     //Irrelevant Code. Removed for readability. 

    //Return the XML
    function retrieve_gk_xml() {
          $xml = new SimpleXMLElement($this->create_gk_xml(), 0, true); //Line 114
          return $xml->xpath('/feed/results/sponsored/listing');
    }
}

这是错误:


PHP致命错误:未找到类'GenieKnows_Search\SimpleXMLElement'

/var/www/myticketpick.com/wp-content /plugins/genieknows-search/genieknows_search.php
在第114行上

PHP Fatal error: Class 'GenieKnows_Search\SimpleXMLElement' not found in /var/www/myticketpick.com/wp-content/plugins/genieknows-search/genieknows_search.php on line 114

看起来它正在尝试查找SimpleXML类在我的GenieKnows_Search命名空间,但是我为什么失去了。

It would appear that it's trying to look for the SimpleXML class inside of my GenieKnows_Search namespace, however I'm at a loss as to why.

有关此错误发生原因的任何想法,以及我如何解决它?

Any ideas on why this error is occurring, and how I can fix it?

推荐答案

修复是我需要在SimpleXmlElement前添加一个正斜杠():

The fix was that I needed to add a forward slash () in front of SimpleXmlElement:

 $xml = new \SimpleXMLElement($this->create_gk_xml(), 0, true);

这里

这篇关于在使用命名空间时找不到SimpleXMLElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆