Redeclaring a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MrRSMan
Forum Newbie
Posts: 20
Joined: Sun Feb 03, 2008 8:11 am

Redeclaring a function

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Redeclaring a function

Post by Benjamin »

No, this approach is not correct. Can you post your code please? We will need to advise you on the correct approach.
MrRSMan
Forum Newbie
Posts: 20
Joined: Sun Feb 03, 2008 8:11 am

Re: Redeclaring a function

Post 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);

}

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Redeclaring a function

Post 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);

}


MrRSMan
Forum Newbie
Posts: 20
Joined: Sun Feb 03, 2008 8:11 am

Re: Redeclaring a function

Post 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 :)
Post Reply