PHP警告:调用时传递引用已被弃用 [英] PHP warning: Call-time pass-by-reference has been deprecated

查看:272
本文介绍了PHP警告:调用时传递引用已被弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到警告:Call-time pass-by-reference has been deprecated用于以下代码行:

I am getting the warning: Call-time pass-by-reference has been deprecated for the following lines of code:

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;
}

这是什么原因以及如何解决?

What does it cause and how to fix it?

推荐答案

从任何地方从&$this删除&,这不是必需的.实际上,我认为您可以在此代码中的所有位置删除&-完全不需要.

Remove & from &$this everywhere, it is not needed. In fact, I think you can remove & everywhere in this code - it is not needed at all.

详细解释

PHP允许通过两种方式传递变量:按值"和按引用".第一种方式(按值")不能修改,而第二种方式(按引用")可以:

PHP allows to pass variables in two ways: "by value" and "by reference". First way ("by value"), you can't modify them, other second way ("by reference") you can:

     function not_modified($x) { $x = $x+1; }
     function modified(&$x) { $x = $x+1; }

请注意&符号.如果我在变量上调用modified,它将被修改,如果我调用not_modified,则它返回的参数值将是相同的.

Note the & sign. If I call modified on a variable, it will be modified, if I call not_modified, after it returns the value of the argument will be the same.

较旧版本的PHP通过执行以下操作允许使用not_modified模拟modified的行为:not_modified(&$x).这是通过引用传递时间".已弃用,永远不要使用.

Older version of PHP allowed to simulate behavior of modified with not_modified by doing this: not_modified(&$x). This is "call-time pass by reference". It is deprecated and should never be used.

此外,在非常古老的PHP版本(阅读:PHP 4及更低版本)中,如果修改对象,则应通过引用将其传递,从而使用&$this.不再需要也不建议这样做,因为在将对象传递给函数时总是对其进行修改,即:

Additionally, in very ancient PHP versions (read: PHP 4 and before), if you modify objects, you should pass it by reference, thus the use of &$this. This is neither necessary nor recommended anymore, as object are always modified when passed to function, i.e. this works:

   function obj_modified($obj) { $obj->x = $obj->x+1; }

这将修改$obj->x,即使它是按值"形式正式传递的,但传递的是对象句柄(如Java等),而不是对象副本(如PHP 4中那样).

This would modify $obj->x even though it formally is passed "by value", but what is passed is object handle (like in Java, etc.) and not the copy of the object, as it was in PHP 4.

这意味着,除非您做一些奇怪的事情,否则几乎不需要传递对象(因此,通过引用调用$this,无论是调用时间还是其他方式).特别是,您的代码不需要它.

This means, unless you're doing something weird, you almost never need to pass object (and thus $this by reference, be it call-time or otherwise). In particular, your code doesn't need it.

这篇关于PHP警告:调用时传递引用已被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆