在使用命名空间时找不到SimpleXMLElement [英] SimpleXMLElement cannot be found when working with namespaces
问题描述
我试图在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屋!