Results 1 to 5 of 5

Thread: [RESOLVED] XML DOMDocument->load not working

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: XML DOMDocument->load not working

    Works perfectly, I went with your last method.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: XML DOMDocument->load not working

    Good choice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width