2
votes

Moving from PHP 7.4 to PHP 8.0, I've got a problem with some code throwing a warning. Code works, but I would like to figure out the problem. There were no Warnings in PHP 7.4. Here are the Warnings: (modified to take my info out of the error)

PHP Warning: XML::open(): Argument #1 ($parser) must be passed by reference, value given in .../classes/xml_5.php on line 89

PHP Warning: XML::open(): Argument #1 ($parser) must be passed by reference, value given in .../classes/xml_5.php on line 89

PHP Warning: XML::open(): Argument #1 ($parser) must be passed by reference, value given in .../classes/xml_5.php on line 89

PHP Warning: XML::data(): Argument #1 ($parser) must be passed by reference, value given in .../classes/xml_5.php on line 89

PHP Warning: XML::close(): Argument #1 ($parser) must be passed by reference, value given in .../classes/xml_5.php on line 89

PHP Warning: XML::open(): Argument #1 ($parser) must be passed by reference, value given in .../classes/xml_5.php on line 89

(they keep going on the same)

The code:

    function __construct(){
        $this->parser = xml_parser_create();
        xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, 'open', 'close');
        xml_set_character_data_handler($this->parser, 'data');
    }

    function destruct(){ xml_parser_free($this->parser); }

    function & parse(&$data){
        $this->document = array();
        $this->stack    = array();
        $this->parent   = &$this->document;
        $return_data = xml_parse($this->parser, $data, true) ? $this->document : NULL;     
        return $return_data;
    }

The problem line (89) is at the end, this line:

$return_data = xml_parse($this->parser, $data, true) ? $this->document : NULL;  

I see that in PHP 8 that xml_parse changed: 8.0.0 parser expects an XMLParser instance now; previously, a resource was expected.

I have spent days on this, and I am missing something! Thanks, everyone!

1
Can you var_dump($this->parser) just before the offending line. What do you get? - BT643
The exact response is "object(XMLParser)#37 (0) { }" - Brandon S
The code you've shown doesn't match your error message. The error is talking about a function called open, so the two pieces of code we need to see are 1) where you define that function, and 2) where you call that function (which is the line that's giving the error message). - IMSoP

1 Answers

-1
votes

I think I figured it out, but I don't have a good explanation; if someone could explain, I would appreciate it.

So line 89 xml_parse($this->parser, $data, true) Calls other functions on the page (leaving the bulk of the code out)

function open(&$parser, $tag, $attributes){...
function data(&$parser, $data){...
function close(&$parser, $tag){....

When I changed the code to:

function open($parser, $tag, $attributes){..
function data($parser, $data){...
function close($parser, $tag){..

The problem was solved.

So to pass a variable by reference, you add the "&" sign. But It had the "&", so I tried removing it, and it worked. So it seems to me it is now not passed by referance. But the error was "Must be passed by reference".

Can anyone explain this?