You are here:  » Using functions as a handler for magic parser

Support Forum



Using functions as a handler for magic parser

Submitted by griffinsbridge on Mon, 2008-02-18 14:31 in

Hi
I don't specifically have a problem, more of an observation

I've checked the forums and no-one seems to have had this issue, so I thought I'd share incase anyone had it in the future.

Im using a switch to decide what to do with my data. This switch points to different functions that either parse my feeds (using MagicParser) or clean up after itself (delete old rows, optimise the tables etc).

Now, what I've found is that ANY globals I want to use in the function myRecordHandler() MUST be global in the function calling for MagicParser.

This is despite those variables being created by the calling function.

does that make sense?

demo:

<?php
  
function doParsing(){
  
$Variable "xyz";
  
MagicParser_parse("Filename.xml","myRecordhandler","xml|STUFF/MORESTUFF/EVENMORESTUFF/");
  }
?>

This would not allow $Variable to be used in myRecordHandler, whether I set the global in myRecordHandler or not. However:

<?php
  
function doParsing(){
   global 
$Variable;
  
$Variable "xyz";
  
MagicParser_parse("Filename.xml","myRecordhandler","xml|STUFF/MORESTUFF/EVENMORESTUFF/");
  }
?>

Would allow myRecordHandler() to use $Variable, so long as I set it to global in myRecordHandler()

Hope this saves people some time in the future

Submitted by support on Mon, 2008-02-18 14:38

Hi,

Yes - that makes sense. Every function that needs access to a global variable must have that variable declared as global, otherwise you will only be accessing a local copy.

The record handler function is not related in any way to the function in which you call MagicParser_parse() - so if you want access to the same variable in both locations then yes, that variable must be declared as global in both locations.

Cheers,
David.