|
-
Jul 11th, 2010, 03:38 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] XML DOMDocument->load not working
I am getting an error message:
Fatal error: Call to a member function load() on a non-object in /home/content/u/l/s/56546/html/myName/SiteEditor/xmlDOMDocHandler.php on line 14
Line 14 is this line:
Code:
$doc->load($configFilePath);
Full Code of file xmlDOMDocHandler.php:
Code:
<?php
$skip = true;
$user="";
if(isset($_SESSION['loggedOn']) && $_SESSION['loggedOn'] == "true"){
$skip = false;
$user=$_SESSION['username'];
}
$configFilePath = 'users/'.$user.'/config.xml' . rand(1,10000);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
//Functions------------------------------------------
function loadXML(){
if($skip){return;}
$doc->load($configFilePath);
}
function setSiteName($newName){
if($skip){return;}
loadXML();
$doc->getElementsByTagName('websitename')->nodeValue = $newName;
$doc->save($configFilePath);
}
function getSiteName(){
if($skip){return;}
loadXML();
return $doc->getElementsByTagName('websitename')->nodeValue;
}
loadXML();
?>
The above file is being included in another file called index.php within an If..Statement. I didn't understand the error message so I was playing around with the code trying to get it to work, that's why it may seem as if I just complicated it too much... (e.g. I don't really need the $skip variable and a few other things)..
Why am I getting the error message? When I tried replacing this code:
Code:
$doc = new DOMDocument();
with
Code:
$doc = DOMDocument::load($configFilePath);
it worked... except the next function, the $doc->getElementsByTagName... returned the same error message.. It's like the method doesn't exist, except I know it exists, I've used it before.
-
Jul 11th, 2010, 04:09 PM
#2
Re: XML DOMDocument->load not working
because you are using a bunch of functions. global scope != function scope.
PHP Code:
$var = "hello";
echo $var; // prints hello
function echoVar(){
echo $var; // prints nothing -- $var doesn't exist in this scope
}
which means that your other variable, $skip, does not exist in these functions' scopes either. you have a few options. the worst, in my opinion, method:
PHP Code:
function echoVar(){
global $var;
echo $var; // prints hello
}
use the globals array, which is just as bad a method (again, in my opinion):
PHP Code:
function echoVar(){
echo $GLOBALS['var']; // prints hello
}
maybe some object-oriented flavour?
PHP Code:
class MyVar {
protected $_var;
public function __construct($var){
// store var $this->_var = $var;
}
public function echoVar(){ echo $this->_var; }
}
$myVar = new MyVar('hello');
$myVar->echoVar(); // prints hello
or, if you just want dead simple, pass the variables in:
PHP Code:
function echoVar($var){
echo $var;
}
echoVar($var); // prints hello
read more about variable scope.
-
Jul 11th, 2010, 04:27 PM
#3
Thread Starter
Frenzied Member
Re: XML DOMDocument->load not working
Thanks I'll rewrite my code and tell you how it works out.
Last edited by noahssite; Jul 11th, 2010 at 04:33 PM.
-
Jul 11th, 2010, 06:06 PM
#4
Thread Starter
Frenzied Member
Re: XML DOMDocument->load not working
Works perfectly, I went with your last method.
-
Jul 11th, 2010, 09:38 PM
#5
Re: XML DOMDocument->load not working
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|