Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument #1 ($parser) must be passed by reference

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!

like image 547
Brandon S Avatar asked Aug 11 '26 13:08

Brandon S


2 Answers

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?

like image 98
Brandon S Avatar answered Aug 14 '26 17:08

Brandon S


just remove or comment this

//curl_setopt($curl, CURLOPT_HEADERFUNCTION, array($this, '__responseHeaderCallback'));

this is work for me I upload with s3 classes in laravel

like image 22
Muhib Trendcas Avatar answered Aug 14 '26 18:08

Muhib Trendcas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!