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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4665782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:54:16  来源:igfitidea点击:

PHP warning: Call-time pass-by-reference has been deprecated

php

提问by yohannan_sobin

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

我收到警告: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;
}

What does it cause and how to fix it?

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

回答by StasM

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

&&$this任何地方删除,它是不需要的。事实上,我认为您可以删除&此代码中的任何地方 - 根本不需要它。

Long explanation

长解释

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:

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

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

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

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

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

老版本的PHP允许模拟的行为modifiednot_modified通过这样做:not_modified(&$x)。这是“调用时通过引用传递”。它已被弃用,不应使用。

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:

此外,在非常古老的 PHP 版本中(阅读:PHP 4 及更早版本),如果您修改对象,您应该通过引用传递它,因此使用&$this. 这既不必要也不推荐,因为对象在传递给函数时总是被修改,即这有效:

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

This would modify $obj->xeven 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.

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

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

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

回答by Bailey Parker

Just in case you're wondering, a call-time pass by reference is a deprecated PHP feature that promotes PHP loose typing. Basically, it allows you to pass a reference (sort of like a C pointer) to a function that has not specifically asked for one. It's PHP's solution to the square peg in a round hole problem.
In you case, neverreference $this. Outside of a class, a reference to its $thiswill not allow you to access it's private methods and fields.

以防万一您想知道,调用时传递引用是一个不推荐使用的 PHP 特性,它促进了 PHP 松散类型。基本上,它允许您将引用(有点像 C 指针)传递给没有特别要求的函数。这是 PHP 对圆孔问题中方钉的解决方案。
在你的情况下,永远不要引用$this. 在类之外,对其的引用$this将不允许您访问它的私有方法和字段。

Example:

例子:

<?php
function test1( $test ) {} //This function doesn't want a reference
function test2( &$test ) {} //This function implicitly asks for a reference

$foo = 'bar';
test2( $foo ); //This function is actually given a reference
test1( &$foo ); //But we can't force a reference on test1 anymore, ERROR
?>