使用命名空间时无法找到SimpleXMLElement

时间:2011-08-24 17:53:45

标签: php wordpress class namespaces simplexml

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

以下是代码:

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行

看起来它正试图在我的GenieKnows_Search命名空间中查找SimpleXML类,但是我不知道为什么。

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

2 个答案:

答案 0 :(得分:37)

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

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

概述here.

答案 1 :(得分:0)

如果您使用的是PHP 5.6,请尝试:

pip install php-simplexml / pip install php-xml

相关问题