传递引用错误
我有一个钩子系统设置...它正在本地主机上工作...我将其投入使用并收到一条错误消息“警告:调用时间传递引用已被弃用”。
现在,显然解决方法是删除所有“&”从您的函数调用,即 foo(&$me) 到 foo($me),然后在 foo 的函数定义中执行“function foo(&$me)”。
但是,我不能这样做......因为我的钩子接受数组作为参数,我需要解决这个问题。就像我可以使用“run_hooks('hook-name',$me);”或“run_hooks ('钩子名称', array ($me, $another_var, 等等...))”;
所以这意味着我不能使用“function run_hooks ( $hook_name, &$arguments )”,因为我会在 php 中收到一个错误,说它不能传递“array()”作为参考......
任何想法和解决方法?
谢谢。
I have a hook system setup... which is working on localhost... I put it live and get an error saying "Warning: Call-time pass-by-reference has been deprecated".
Now, apparently the work around is to remove all "&" from your function calls, ie foo(&$me) to foo($me) and then in foo's function definition do "function foo(&$me)".
However, I can not do this... because my hooks accept an array as arguments, I need a work around for this. Like I can use "run_hooks ( 'hook-name', $me );" or "run_hooks ( 'hook-name', array ( $me, $another_var, etc... ) )";
So this means I can not use "function run_hooks ( $hook_name, &$arguments )" because I'll get an error in php saying it can not pass "array()" as reference...
Any ideas an a work around?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您想使用引用来防止不必要的数据复制。但这不是你应该使用它们的目的(在 php5 中)。只需传递数组即可。
只要您不更改数组,这不会调用作为参数传递的数组的深层副本。这种机制称为写时复制,PHP 实现在 http://www.research.ibm.com/trl/people/mich/pub/200901_popl2009phpsem.pdf
I guess you want to use a reference in order to prevent unnecessary copies of data. But that's not what you should use them for (in php5). Simply pass the array.
This invokes no deep copy of the array that is passed as parameter - as long as you don't change the array. This mechanism is called copy-on-write and the php implementation is explained in http://www.research.ibm.com/trl/people/mich/pub/200901_popl2009phpsem.pdf
传递一个引用数组 - 该数组本身不需要是一个引用:
Pass an array of references around - this array itself doesn't need to be a reference:
我认为我找到了一个可行的解决方法,没有错误,并且它确实可以在不修改我的类中的任何内容的情况下工作...可以帮助其他任何人... ArrayObject 解决了它,因为对象已经被“引用”。
I think I found a viable workaround, no errors, and it does work without modifying anything in my classes... could help anyone else... ArrayObject solved it, seince objects are already 'referenced'.