Jump to content



Photo
- - - - -

Call to a member function on a non-object


  • Please log in to reply
4 replies to this topic

#1   mugitty

mugitty
  • Members
  • 1,201 posts
  • Real Name:Stuart Owens
  • Location:Great Southeastern Washington Desert, USA

Posted 01 November 2004 - 01:12

Any insight from someone who is familiar proper calls to objects would be much appreciated...

This problem has just started appearing recently in the xmldocument.php file that is part of the UPSXML shipping contribution. I know that it's appearing because an object doesn't appear to be registered, but it happens randomly and I'm not sure what to do to correct it (or why it is random, rather than consistent if the code is poorly written).

The error is:

Fatal error: Call to a member function on a non-object in /home/farmex/public_html/shop/includes/classes/xmldocument.php on line 57

I'm posting the entire file below, but breaking it into 3 segments. The segment with the offending line (57) is in the middle and the line reads

if ($pathArray[0] == $this->root->getName()) {

<?php
/*
  $Id: xmldocument.php,v 1.5 2003/06/27 01:03:03 torinwalker Exp $

  Written by Torin Walker
  torinwalker@rogers.com
  
  Generic XML Document support for when there is none.
  
  Copyright(c) 2003 by Torin Walker, All rights reserved.

  Released under the GNU General Public License
*/

define("ELEMENT", 0);
define("TEXTELEMENT", 1);

class XMLDocument {
  var $root;
  var $children;

  function XMLDocument() {
  }

  function createElement($name) {
    $node = new Node();
    $node->setName($name);
    $node->setType(ELEMENT);
    return $node;
  }

  function createTextElement($text) {
    $node = new Node();
    $node->setType(TEXTELEMENT);
    $node->setValue($text);
    return $node;
  }

  function getRoot() {
    return $this->root;
  }
  
  function setRoot($node) {
    $this->root = $node;
  }

  function toString() {
    if ($this->root) {
      return $this->root->toString();
    } else {
      return "DOCUMENT ROOT NOT SET";
    }
  }
The problem section...
 function getValueByPath($path) {
    $pathArray = split("/", $path);
    if ($pathArray[0] == $this->root->getName()) {
      //print_r("Looking for " . $pathArray[0] . "<br>");
      array_shift($pathArray);
      $newPath = implode("/", $pathArray);
      return $this->root->getValueByPath($newPath);
    }
  }
... and the balance of the file
}

class Node {
  var $name;
  var $type;
  var $text;
  var $parent;
  var $children;
  var $attributes;

  function Node() {
    $this->children = array();
    $this->attributes = array();
  }
    
  function getName() {
    return $this->name;
  }

  function setName($name) {
    $this->name = $name;
  }

  function setParent(&$node) {
    $this->parent =& $node;
  }

  function &getParent() {
    return $this->parent;
  }

  function &getChildren() {
    return $this->children;
  }

  function getType() {
    return $this->type;
  }

  function setType($type) {
    $this->type = $type;
  }

  function getElementByName($name) {
    for ($i = 0; $i < count($this->children); $i++) {
      if ($this->children[$i]->getType() == ELEMENT) {
        if ($this->children[$i]->getName() == $name) {
          return $this->children[$i];
        }
      }
    }
    return null;
  }

  function getElementsByName($name) {
    $elements = array();
    for ($i = 0; $i < count($this->children); $i++) {
      if ($this->children[$i]->getType() == ELEMENT) {
        if ($this->children[$i]->getName() == $name) {
          $elements[] = $this->children[$i];
        }
      }
    }
    return $elements;
  }

  function getValueByPath($path) {
    $pathArray = split('/', $path);
    $node = $this;
    for ($i = 0; $i < count($pathArray); $i++) {
      //print_r("Looking for " . $pathArray[$i] ."<br>");
      if ($node->getChildren()) {
        for ($j = 0; $j < count($node->getChildren()); $j++) {
        if ($node->children[$j]->getType() == ELEMENT) {
          if ($node->children[$j]->getName() == $pathArray[$i]) {
              //print_r("Found " . $pathArray[$i] ."<br>");
              $node = $node->children[$j];
            }
          }
        }
      }
    }
    return $node->getValue();
  } 

  function getText() {
    return $this->text();
  }

  function setValue($text) {
    $this->text = $text;
  }

  function getValue() {
    $value = NULL;
    if ($this->getType() == ELEMENT) {
      for ($i = 0; $i < count($this->children); $i++) {
        $value .= $this->children[$i]->getValue();
      }
    } elseif ($this->getType() == TEXTELEMENT) {
      $value .= $this->text;
    }
    return $value;
  }

  function setAttribute($name, $value) {
    $attributes[$name] = $value;
  }

  function getAttribute($name) {
    return $attributes[$name];
  }

  function addNode(&$node) {
    $this->children[] =& $node;
    $node->parent =& $this;
  }

  function parentToString($node) {
    while($node->parent) {
      //print_r("Node " . $node->name . " has parent<br>");
      $node = $node->parent;
    }
    //print_r("Node contents from root: " . $node->toString() . "<br>");
  }

  function toString() {
    $string = NULL;

    //print_r("toString child count " . $this->name . " contains " . count($this->children) . "<br>");

    if ($this->type == ELEMENT) {
      $string .= '{' . $this->name . '}';
      for ($i = 0; $i < count($this->children); $i++) {
        $string .= $this->children[$i]->toString();
      }
      $string .= '{/' . $this->name . '}';
    } else {
      $string .= $this->getValue();
    }
    return $string;
  }

}

class XMLParser {
  var $xp;
  var $document;
  var $current;
  var $error;

  // Constructor
  function XMLParser() {
    $this->document = new XMLDocument();
    $this->error = array();
  }

  function setDocument($document) {
    $this->document = $document;
  }

  function getDocument() {
    return $this->document;
  }

  // Destructor
  function destruct(){
    xml_parser_free($this->xp);
  }

  // return 1 for an error, 0 for no error
  function hasErrors() {
    if (sizeof($this->error) > 0) {
      return 1;
    } else {
      return 0;
    }
  }

  // return array of error messages
  function getError() {
    return $this->error;
  }

  // process xml start tag
  function startElement($xp, $name, $attrs) {
    //print_r("Found Start Tag: " . $name . "<br>");
    $node =& $this->document->createElement($name);

    if ($this->document->getRoot()) {
      $this->current->addNode($node);
    } else {
      $this->document->root =& $node;
    }
    $this->current =& $node;
  }

  // process xml end tag
  function endElement($xp, $name){
    //print_r("Found End Tag: " . $name . "<br>");
    if ($this->current->getParent()) {
      $this->current =& $this->current->getParent();
    }
  }

  // process data between xml tags
  function dataHandler($xp, $text) {
    //print_r("Adding Data: \"" . $text . "\"<br>");
    $node =& $this->document->createTextElement($text);
    $this->current->addNode($node);
  }

  // parse xml document from string
  function parse($xmlString) {
    if(!($this->xp = @xml_parser_create())) {
      $this->error['description'] = 'Could not create xml parser';
    }
    if(!$this->hasErrors()) {
      if(!@xml_set_object($this->xp, $this)) {
        $this->error['description'] = 'Could not set xml parser for object';
      }
    }
    if(!$this->hasErrors()) {
      if(!@xml_set_element_handler($this->xp, 'startElement', 'endElement')) {
        $this->error['description'] = 'Could not set xml element handler';
      }
    }
    if(!$this->hasErrors()) {
      if(!@xml_set_character_data_handler($this->xp, 'dataHandler')) {
        $this->error['description'] = 'Could not set xml character handler';
      }
    } 

  xml_parser_set_option($this->xp, XML_OPTION_CASE_FOLDING, false);

    if (!$this->hasErrors()) {
      if(!@xml_parse($this->xp, $xmlString)) {
        $this->error['description'] = xml_error_string(xml_get_error_code($this->xp));
        $this->error['line'] = xml_get_current_line_number($this->xp);
      }
    }
  }
}
?>

... if you want to REALLY see something that doesn't set up right out of the box without some tweaking,
try being a Foster Parent!

#2   MikeMike

MikeMike
  • Members
  • 154 posts
  • Real Name:Michael

Posted 05 November 2004 - 14:35

Just wonder if you still are getting that error?
I haven't seen anyone mention it since last weekend and I switched my own site to the regular UPS module. Could it have been an error from the UPS server?

#3   mix

mix
  • Members
  • 4 posts
  • Real Name:mix

Posted 30 October 2005 - 14:00

I have exactly the same problem and I don't find why because it happens only sometimes... mugitty > did you find the reason ?
Anyone have an idea ?

#4   xao

xao
  • Members
  • 54 posts
  • Real Name:Tony

Posted 03 March 2006 - 07:14

BUMP....

I just started getting this error too, and it seems to go fine for a while, then all of a sudden a ton of them in a row.

What's bad about this, is that it presents a blank page on the checkout process until you hit reload. :/

Anyone figure this out?

#5 ONLINE   Jan Zonjee

Jan Zonjee

    Governor of Peace

  • Core Team
  • 7,093 posts
  • Real Name:Jan Zonjee
  • Gender:Male
  • Location:the Netherlands

Posted 03 March 2006 - 12:05

Anyone figure this out?

Yes, this was "fixed" (more like neutralized) in the newer UPS XML versions. See line 808-814 of catalog/includes/modules/shipping/upsxml.php, function _post:

if(!$xmlResponse || strstr(strtolower(substr($xmlResponse, 0, 120)), "bad request"))  {
			/* Sometimes the UPS server responds with an HTML message (differing depending on whether the test server
			   or the production server is used) but both have in the title tag "Bad request".
			   Parsing this response will result in a fatal error:
			   Call to a member function on a non-object in /blabla/includes/classes/xmldocument.php on line 57
			   It only results in not showing Estimated Delivery Dates to the customer so avoiding the fatal error should do.
			*/