Page 1 of 1
Redeclaring a function
Posted: Sun Jun 13, 2010 6:18 am
by MrRSMan
I'm trying to use foreach to pass $var to foo(), and I get "Fatal error: Cannot redeclare foo()". I understand why I'm getting this error since PHP does not allow you to declare a function more than once, but this is exactly what I need to do. Can I make foreach somehow "wipe clean" foo() each time so it can be declared again?
Thank you in advance.
Re: Redeclaring a function
Posted: Sun Jun 13, 2010 7:05 am
by Benjamin
No, this approach is not correct. Can you post your code please? We will need to advise you on the correct approach.
Re: Redeclaring a function
Posted: Sun Jun 13, 2010 8:31 am
by MrRSMan
Okay my code is fairly simple I think. I'm using an XML parser (which works fine), but want to run it for every instance of $nation.
Code: Select all
$nations = array("standica", "alecstonia");
foreach($nations as $nation){
//XML parser start
$uFile = "http://www.nationstates.net/cgi-bin/regiondata.cgi/nation=$nation";
function startElement($parser, $name, $attrs) {
global $curTag;
$curTag .= "^$name";
}
function endElement($parser, $name) {
global $curTag;
$caret_pos = strrpos($curTag,'^');
$curTag = substr($curTag,0,$caret_pos);
}
function characterData($parser, $data) {
global $curTag;
global $type;
if ($curTag == "^NATIONINFO^TYPE") {
$type = $data;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open NATIONINFO for input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
//XML parser end
print($type);
}
Re: Redeclaring a function
Posted: Sun Jun 13, 2010 9:19 am
by Benjamin
Okay. So you want to remove the functions from the foreach loop, and instead simply call them from within the loop. Something like this:
Code: Select all
<?php
function startElement($parser, $name, $attrs) {
global $curTag;
$curTag .= "^$name";
}
function endElement($parser, $name) {
global $curTag;
$caret_pos = strrpos($curTag,'^');
$curTag = substr($curTag,0,$caret_pos);
}
function characterData($parser, $data) {
global $curTag;
global $type;
if ($curTag == "^NATIONINFO^TYPE") {
$type = $data;
}
}
$nations = array("standica", "alecstonia");
foreach($nations as $nation){
//XML parser start
$uFile = "http://www.nationstates.net/cgi-bin/regiondata.cgi/nation=$nation";
startElement($parser, $name, $attrs);
endElement($parser, $name);
characterData($parser, $data);
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open NATIONINFO for input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
//XML parser end
print($type);
}
Re: Redeclaring a function
Posted: Sun Jun 13, 2010 9:39 am
by MrRSMan
Benjamin thank you very much- you've just put an end to 2 days of head scratching and failed solutions. I've just tested what you said and it worked perfectly- your help is hugely appreciated.
Cookies for Benjamin
