SimpleXMLElement cannot be found when working with namespaces
Asked Answered
C

3

16

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

Here is the code:

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');
    }
}

Here is the error:

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

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?

Capricorn answered 24/8, 2011 at 17:53 Comment(0)
C
42

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

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

As outlined here.

Capricorn answered 24/8, 2011 at 18:21 Comment(1)
This does not work for me. I'm using PHP 5.6, libxml, SimpleXML enabled.Wanderjahr
V
1

My app was crashing without any error reports. Finally figured out it was this.

On Ubuntu, run sudo apt install php-xml.

Vigilante answered 31/10, 2022 at 20:40 Comment(0)
S
0

If you are using PHP 5.6, Try:

pip install php-simplexml / pip install php-xml

Scofield answered 10/5, 2020 at 3:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.