Home > other >  Upgrading from PHP 7.4 to 8 - fatal error
Upgrading from PHP 7.4 to 8 - fatal error

Time:06-13

I can't find the solution to this problem that occurs when I upgrade from PHP 7.4 to 8 on my shared hosting site. Can any of you help me?

The message says:

Fatal error: Cannot declare class XMLParser, because the name is already in use in /home/[...]/commonfunctions.php on line 1015

The code from 1015:

    class XMLParser 
{
    var $filename;
    var $xml;
    var $data;

//4 lignes to repair php 7
    public function __construct() 
    {
        //nothing
    } 
    
    
    function XMLParser($xml_file)
    {
        $this->filename = $xml_file;
        $this->xml = xml_parser_create();
        xml_set_object($this->xml, $this);
        xml_set_element_handler($this->xml, 'startHandler', 'endHandler');
        xml_set_character_data_handler($this->xml, 'dataHandler');
        $this->parse($xml_file);
    }

    function parse($xml_file)
    {
        if (!($fp = fopen($xml_file, 'r'))) 
        {
            die('Cannot open XML data file: '.$xml_file);
        return false;
        }

        $bytes_to_parse = 512;

        while ($data = fread($fp, $bytes_to_parse)) 
        {
            $parse = xml_parse($this->xml, $data, feof($fp));
       
            if (!$parse) 
            {
                die(sprintf("XML error: %s at line %d",
                xml_error_string(xml_get_error_code($this->xml)),
                   xml_get_current_line_number($this->xml)));
                xml_parser_free($this->xml);
            }
        }

        return true;
    }

    function startHandler($parser, $name, $attributes)
    {
        $data['name'] = $name;
        if ($attributes) 
        {
            $data['attributes'] = $attributes; 
        }
        $this->data[] = $data;
    }

    function dataHandler($parser, $data)
    {
        if ($data = trim($data)) 
        {
            $index = count($this->data) - 1;
            if(isset($this->data[$index]['content'])) 
            $this->data[$index]['content'] .= $data;
            else $this->data[$index]['content'] = $data;
        }
    }

    function endHandler($parser, $name)
    {
        if (count($this->data) > 1) 
        {
            $data = array_pop($this->data);
            $index = count($this->data) - 1; 
            $this->data[$index]['child'][] = $data;
        }
    }
}

CodePudding user response:

Your class is in conflict with a new native class that was introduced in PHP8. Here link . You need to rename your class otherwise it won't work and also rename everywhere in your code where referencing this class.

That's the explanation of this error message

Fatal error: Cannot declare class XMLParser, because the name is already in use in
  •  Tags:  
  • php
  • Related