如何避免PHP中的调用时不通过引用传递错误? [英] How to avoid call-time pass-by-reference deprecated error in PHP?

查看:95
本文介绍了如何避免PHP中的调用时不通过引用传递错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一个警告是:

不推荐使用呼叫时间传递.

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

我很难想象为什么不赞成使用它,因为它是一种有用的编程功能,基本上,我这样做:

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

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

    ...
}

,我反复调用此函数,从结果中获取结果并进行处理,但也通过重复发送该方法使数组$ lines建立起来.

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.

  1. 要重新编程,这将是很广泛的.
  2. 我不想只是关闭警告",因为我想查看其他警告.

因此,不赞成使用按引用调用,实现此模式的功能的可接受方式"是什么:即将字符串数组发送到方法中,让它们成为方法改变了,然后继续使用该数组?

So, as call-by-reference is deprecated, what is the "accepted way" to attain the functionality of 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?

推荐答案

实际上,定义函数的方式没有问题.调用函数的方式有问题.因此,对于您的示例,不必像这样调用它:

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

您可以这样称呼它:

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

因此该功能仍然可用.但我不知道此更改的原因.

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

这篇关于如何避免PHP中的调用时不通过引用传递错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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