Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

php - 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!

question from:https://stackoverflow.com/questions/65947307/argument-1-parser-must-be-passed-by-reference

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...