Haritz
Haritz

Reputation: 1752

DOMDocument and dom module in PHP

I am developing an application with php where I need to use XML files and transform them by using a XSL style sheet. To load the XML and XSL files I use the following:

$xsl = new DOMDocument();
$xsl->load("myxslfile.xsl");  // load XSL
$xml = new DOMDocument(); 
$xml->load("myxmlfile.xml");  // load XML

the problem is when running the code the next error appears:

Fatal error: Class 'MyDirectory\DOMDocument' not found in 'mydirectory'

Running phpinfo() I can see dom is intalled and enabled. I don't know why I can't run DOMDocument()

Upvotes: 2

Views: 1153

Answers (1)

Gordon
Gordon

Reputation: 316939

You are apparently using the namespace MyDirectory. But DOMDocument is a class in the global namespace. To reference a class from the global namespace in a local namespace you have to use the Fully Qualified Name or import it into the local namespace:

Fully Qualified Namespace:

$xsl = new \DOMDocument();
$xsl->load("myxslfile.xsl");  // load XSL
$xml = new \DOMDocument(); 
$xml->load("myxmlfile.xml");  // load XML

Importing with use

namespace MyDirectory;
use \DOMDocument;

$xsl = new DOMDocument();
$xsl->load("myxslfile.xsl");  // load XSL
$xml = new DOMDocument(); 
$xml->load("myxmlfile.xml");  // load XML

Upvotes: 3

Related Questions