传递引用错误

发布于 2024-08-26 04:16:26 字数 461 浏览 10 评论 0原文

我有一个钩子系统设置...它正在本地主机上工作...我将其投入使用并收到一条错误消息“警告:调用时间传递引用已被弃用”。

现在,显然解决方法是删除所有“&”从您的函数调用,即 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面如桃花 2024-09-02 04:16:26

我猜您想使用引用来防止不必要的数据复制。但这不是你应该使用它们的目的(在 php5 中)。只需传递数组即可。

function foo($x /* not a reference */) {
  echo $x['bar'], "\n";
} 
$p = array('bar'=>12345);
foo($p);
// or
foo( array('bar'=>12345) );

只要您不更改数组,这不会调用作为参数传递的数组的深层副本。这种机制称为写时复制,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.

function foo($x /* not a reference */) {
  echo $x['bar'], "\n";
} 
$p = array('bar'=>12345);
foo($p);
// or
foo( array('bar'=>12345) );

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

沒落の蓅哖 2024-09-02 04:16:26

传递一个引用数组 - 该数组本身不需要是一个引用:

 function foo($args) {
    modify $args[0]...
    modify $args[1]...
 }

 $someVar = ...
 $anotherVar = ...

 foo(array(&$someVar, &$anotherVar));

Pass an array of references around - this array itself doesn't need to be a reference:

 function foo($args) {
    modify $args[0]...
    modify $args[1]...
 }

 $someVar = ...
 $anotherVar = ...

 foo(array(&$someVar, &$anotherVar));
戒ㄋ 2024-09-02 04:16:26

我认为我找到了一个可行的解决方法,没有错误,并且它确实可以在不修改我的类中的任何内容的情况下工作...可以帮助其他任何人... ArrayObject 解决了它,因为对象已经被“引用”。

function test ( $var, $foo )
{
    $var    = 3;
    $foo    = 3;
}

$var    = 1;
$foo    = 1;
call_user_func_array ( 'test', new ArrayObject ( array ( &$var, &$foo ) ) );

print $foo;
print $var;

// Output is 3 3 like expected :)

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'.

function test ( $var, $foo )
{
    $var    = 3;
    $foo    = 3;
}

$var    = 1;
$foo    = 1;
call_user_func_array ( 'test', new ArrayObject ( array ( &$var, &$foo ) ) );

print $foo;
print $var;

// Output is 3 3 like expected :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文