You are here:  » Is it possible to call the handler from within a class?


Is it possible to call the handler from within a class?

Submitted by buyowner on Thu, 2008-11-13 16:10 in

I have a class to handle the parsing, and was wondering how I can call a "class function" within the class

class parser_class {
 var $feedId;
 var $feedPath;
 var $feedData;
function getFeed(){
  $xml = "";
  $url = $this->feedPath;
  if ($fp = fopen($url,"r"))
  {
    while(!feof($fp)) $xml .= fread($fp,1024);
    fclose($fp);
  }
  $this->feedData = $xml;
}
function getFeedId() {
 $result = MagicParser_parse("string://".$this->feedData,"feedIdHandler","xml|TRUEMLS/");
 }
function feedIdHandler($record)
  {
    $this->feedId = $record["TRUEMLS-COMPANY_ID"];
  }
}

Submitted by support on Thu, 2008-11-13 16:16

Hi,

That's not possible i'm afraid because Magic Parser uses global variables internally.

It is easily worked around however, as Magic Parser is not re-entrant (it doesn't return from MagicParser_parse() until the parse has completed); so you can just use global variables to transfer the information required from within your record handler function (which must be defined outside the class) back into your class. So, based on your example above, a slight re-arrangement should do the trick:

// record handler function outside the class
function myFeedIDRecordHandler($record)
{
  global $tempFeedId;
  $tempFeedId = $record["TRUEMLS-COMPANY_ID"];
}
class parser_class {
 var $feedId;
 var $feedPath;
 var $feedData;
function getFeed(){
  $xml = "";
  $url = $this->feedPath;
  if ($fp = fopen($url,"r"))
  {
    while(!feof($fp)) $xml .= fread($fp,1024);
    fclose($fp);
  }
  $this->feedData = $xml;
}
 function getFeedId() {
 $result = MagicParser_parse("string://".$this->feedData,"myFeedIDRecordHandler","xml|TRUEMLS/");
 global $tempFeedId;
 $this->feedId = $tempFeedId;
}
}

Hope this helps,
Cheers,
David.