"Ölümcül hata: Redeclare"

1 Cevap php

Ben sadece biraz dışarı veri şerit ve devam etmek gerekir gibi birden çok kez aramaya çalışıyorum, tek bir xml ayrıştırma işlevi var.

İşte fonksiyonudur:

//Parse Product ID from Product Sides
function getProductSpecs($xml,$type) {

    // Setup arrary
    global $productspecs;
    global $count;
    $count = 0;
    global $type_check;
    $type_check = $type;

    // Parse the XML
    // Create the parser
    if (! ($xmlparser = xml_parser_create()) )
    { 
     die ("Cannot create name list parser");
    }

    // Start tag function
    function first($parser, $name, $attribs) {
    	global $trigger;
    	if ($name == "PRODUCTSIDEID") {
    		$trigger = 1;
    	} elseif ($name == "PRODUCTID") {
    		$trigger = 1;
    	}
    }

    // data handler function
    function xml($parser, $data) {
    	global $trigger;
    	global $productspecs;
    	global $count;
    	global $type_check;
    	if ($trigger == 1){
    		if ($type_check == "sideid") {
    			$productspecs[$count]=$data;
    			$count = $count + 1;
    		} elseif ($type_check == "productid") {
    			$productspecs[$count]=$data;
    			$count = $count + 1;
    		}				
    		$trigger = 0;
    	}
    }

    // Call the handler functions
    xml_set_element_handler($xmlparser, "first", "");

    // Call the data handler
    xml_set_character_data_handler($xmlparser, "xml");

    // Parse the XML data
    xml_parse($xmlparser,$xml);
    // Clear parser
    xml_parser_free($xmlparser);

    //Return the array
    return $productspecs;
}

Benim sorunum bu adı ne zaman ortaya çıkar:

xml_set_element_handler($xmlparser, "first", "");

Ben Redeclare hatayı alıyorum:

function first($parser, $name, $attribs) {

Fonksiyonu sadece bir kez görünür ve ben sorunu varsayarak kulüpler çağrısı oluşur ama bu etrafında bir yolu ben çok kod çoğaltmak gerekmez yoktur. Ben bu birden çok kez arasında yineleme zorunda gidiyorum.

Teşekkürler.

1 Cevap

Fonksiyonların içinde fonksiyonlarını tanımlayan bu yol açabilir. Çalıştırdığınız her zaman getProductSpecs() o first() ve xml() tekrar ve PHP, her kullanıcı fonksiyonları vardır bildirmek için denemek için gidiyor declared in a global scope. En iyi çözüm, ana getProductSpecs() fonksiyonu dışında first() fonksiyonunu ve xml() fonksiyonu hareket etmektir.

Başka bir seçenek bu gibi function_exists() işlev bildirimleri etrafında kullanmak için:

if (! function_exists('first')) {
 // Start tag function
    function first($parser, $name, $attribs) {
        global $trigger;
        if ($name == "PRODUCTSIDEID") {
                $trigger = 1;
        } elseif ($name == "PRODUCTID") {
                $trigger = 1;
        }
    }
}