PHP警告:调用时传递引用已被弃用

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

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;

}

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

回答:

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

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

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

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

注意&标志。如果我调用modified一个变量,它将被修改,如果我调用not_modified,则在返回值之后,参数的值将是相同的。

通过执行以下操作,较旧版本的PHP可以模拟modifiedwith

not_modified的行为:not_modified(&$x)。这是“通过引用传递时间”。不推荐使用,永远不要使用它。

另外,在非常古老的PHP版本中(阅读:PHP

4和更低版本),如果您修改对象,则应通过引用传递它,从而使用&$this。既不需要也不建议这样做,因为在将对象传递给函数时总是会对其进行修改,即可以:

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

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

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

以上是 PHP警告:调用时传递引用已被弃用 的全部内容, 来源链接: utcz.com/qa/401385.html

回到顶部