调用时间传递引用已弃用:理解和解决方案
PHP 的警告“调用时间传递引用已被弃用” deprecated”表示在函数调用期间使用 & 符号通过引用传递变量的过时用法。这种做法多年来一直不被鼓励,PHP 8 已正式弃用它。
弃用的原因:
在较旧的 PHP 版本中,使用引用传递变量& 符号对于修改作为参数传递的对象是必需的。然而,现代 PHP 版本本身支持按值传递对象,但其行为就像按引用传递一样。这意味着修改函数中的对象也会修改原始对象。
解决方案:
要解决弃用警告并确保代码与未来 PHP 版本的兼容性,请删除通过引用传递变量的代码行中的 & 符号。
在您提供的代码中,可以将 & 符号删除为如下所示:
function XML() { $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 xml_parse($this->parser, $data, true) ? $this->document : NULL; }
其他注意事项:
以上是为什么 PHP 中不推荐使用调用时传递引用以及如何修复它?的详细内容。更多信息请关注PHP中文网其他相关文章!