如何避免 PHP 中的调用时传递引用已弃用错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1055812/
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 00:48:35  来源:igfitidea点击:

How to avoid call-time pass-by-reference deprecated error in PHP?

php

提问by Edward Tanguay

I'm trying to reduce the warningsthat are sent to my apache server log.

我正在尝试减少发送到我的 apache 服务器日志的警告

One warning is:

一个警告是:

Call-time pass-by-reference has been deprecated.

调用时传递引用已被弃用。

It is hard for me to imaginewhy this was deprecated since it is such a useful programming feature, basically I do this:

这是我很难想象为什么这个被废弃了,因为它是这样一个有用的编程功能,基本上我这样做:

public function takeScriptsWithMarker(&$lines, $marker) {

    ...
}

and I call this function repeatedly getting results back from it and processing them but also letting the array $lines build up by being sent into this method repeatedly.

我反复调用这个函数,从它获取结果并处理它们,但也让数组 $lines 通过重复发送到这个方法来建立。

  1. To reprogram this would be extensive.
  2. I don't want to just "turn off warnings" since I want to see other warnings.
  1. 重新编程这将是广泛的。
  2. 我不想只是“关闭警告”,因为我想看到其他警告。

So, as call-by-reference is deprecated, what is the "accepted way" to attain the functionalityof this pattern: namely of sending an array of strings into a method, have them be changed by the method, then continuing to use that array?

因此,由于不推荐使用按引用调用,实现此模式功能的“可接受方式”是什么:即将字符串数组发送到方法中,让方法更改它们,然后继续使用该方法大批?

回答by Ionu? G. Stan

Actually, there's no problem with the way you define the function. Is a problem with the way you call the function. So for your example, instead of calling it like:

实际上,您定义函数的方式没有问题。是你调用函数的方式有问题。所以对于你的例子,而不是像这样调用它:

takeScriptsWithMarker(&$lines, $marker);

You'd call it like:

你会这样称呼它:

takeScriptsWithMarker($lines, $marker); // no ampersands :)

So the feature is still available. But I don't know the reason behind this change.

因此该功能仍然可用。但我不知道这种变化背后的原因。

回答by Jason

like noted above in a previous answer, the issue is at CALL time, not definition time.. so you could define a function as:

就像在前面的答案中提到的那样,问题出在 CALL 时间,而不是定义时间..所以您可以将函数定义为:

function foo(&$var1,$var2,$var3=null){
    // procesing here
}

then call as:

然后调用为:

$return = foo($invar1,$invar2);

your first invar is passed by reference, second one is not.

你的第一个 invar 是通过引用传递的,第二个不是。

the error appears when you try to call like so:

当您尝试像这样调用时会出现错误:

$return = foo(&$invar1,$invar2);

回答by Otto Allmendinger

You can set allow_call_time_pass_referenceto truein your php.inifile. But it's a hack.

您可以在文件中设置allow_call_time_pass_reference为。但这是一个黑客。truephp.ini

回答by jetboy

You could pass an array with a reference in:

您可以传递一个带有引用的数组:

public function takeScriptsWithMarker(array(&$lines, $marker))

which should only take a small amount of refactoring at the other end.

这应该只需要在另一端进行少量重构。

回答by seanmonstar

You could pass in the array, let it manipulate it, and then "return" it, instead of messing with the original reference. It shouldn't be too hard to just include a return and assignment.

您可以传入数组,让它对其进行操作,然后“返回”它,而不是弄乱原始引用。只包含返回和分配应该不会太难。

public function takeScriptsWithMarker($lines, $marker) {
    //...
    return $lines;
}

Usage:

用法:

$lines = takeScriptsWithMarker($lines, $marker);