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!
Answer
Solution:
I think I figured it out, but I don't have a good explanation; if someone could explain, I would appreciate it.
So line 89xml_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?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the process class relies on proc_open, which is not available on your php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.