为什么PHP的call_user_func()函数不支持通过引用传递? [英] Why does PHP's call_user_func() function not support passing by reference?

查看:1887
本文介绍了为什么PHP的call_user_func()函数不支持通过引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么函数处理函数 call_user_func()不支持通过引用传递参数?

Why don't the function handling functions like call_user_func() support passing parameters by reference?

文档说的内容很简单,请注意,call_user_func()的参数不会通过引用传递。我假设PHP开发人员有这种情况下禁用该功能的某种原因。

The docs say terse things like "Note that the parameters for call_user_func() are not passed by reference." I assume the PHP devs had some kind of reason for disabling that capability in this case.

他们面临技术限制吗?它是一种语言设计选择吗?

Were they facing a technical limitation? Was it a language design choice? How did this come about?

编辑:

为了澄清这一点,下面是一个例子。 / p>

In order to clarify this, here is an example.

<?php

function more(&$var){ $var++; }

$count = 0;
print "The count is $count.\n";

more($count);
print "The count is $count.\n";

call_user_func('more', $count);
print "The count is $count.\n";

// Output:
// The count is 0.
// The count is 1.
// The count is 1.

这是正常工作; call_user_func不会通过引用传递$ count,即使more()将其声明为引用的变量。 call_user_func文档清楚地说,这是它应该的工作方式。

This is functioning normally; call_user_func does not pass $count by reference, even though more() declared it as a referenced variable. The call_user_func documentation clearly says that this is the way it's supposed to work.

我很清楚,我可以通过使用 call_user_func_array('more',array(& $ count))获得我需要的效果。

I am well aware that I can get the effect I need by using call_user_func_array('more', array(&$count)).

问题是:为什么是call_user_func 设计这样工作? 传递参考文档说,函数定义本身就足以通过引用正确传递参数。 call_user_func的行为是一个例外。为什么?

The question is: why was call_user_func designed to work this way? The passing by reference documentation says that "Function definitions alone are enough to correctly pass the argument by reference." The behavior of call_user_func is an exception to that. Why?

推荐答案

答案是深入嵌入参考在PHP的模型中的工作方式必须的实现,因为这可能有很多,特别是在5.x版本。我相信你听说过这些行,他们不像C指针,或者C ++引用等等。基本上当一个变量被赋值或绑定时,它可以通过两种方式发生 - 通过值(其中例如,新变量绑定到包含旧值副本的新框),或者通过引用(在这种情况下,新变量绑定到与旧值相同的值框)。

The answer is embedded deep down in the way references work in PHP's model - not necessarily the implementation, because that can vary a lot, particularly in the 5.x versions. I'm sure you've heard the lines, they're not like C pointers, or C++ references, etc etc... Basically when a variable is assigned or bound, it can happen in two ways - either by value (in which case the new variable is bound to a new 'box' containing a copy of the old value), or by reference (in which case the new variable is bound to the same value box as the old value). This is true whether we're talking about variables, or function arguments, or cells in arrays.

当你开始将引用传递给函数时,事情开始变得有点毛茸茸,显然目的是能够修改原来的变量。相当一段时间以前,调用时传递引用(将引用传递给不期望的引用的功能)已被弃用,因为一个不知道它的引用处理引用的函数可能偶然'修改输入。把它带到另一个层次,如果该函数调用第二个函数,它本身不是一个参考...然后一切都被断开了。

Things start to get a bit hairy when you start passing references into functions - obviously the intent is to be able to modify the original variables. Quite some time ago, call-time pass-by-reference (the ability to pass a reference into a function that wasn't expecting one) got deprecated, because a function that wasn't aware it was dealing with a reference might 'accidentally' modify the input. Taking it to another level, if that function calls a second function, that itself wasn't expecting a reference... then everything ends up getting disconnected. It might work, but it's not guaranteed, and may break in some PHP version.

这是 call_user_func(

This is where call_user_func() comes in. Suppose you pass a reference into it (and get the associated the call-time pass-by-reference warning). Then your reference gets bound to a new variable - the parameters of call_user_func() itself. Then when your target function is called, its parameters are not bound where you expect. They're not bound to the original parameters at all. They're bound to the local variables that are in the call_user_func() declaration. call_user_func_array() requires caution too. Putting a reference in an array cell could be trouble - since PHP passes that array with "copy-on-write" semantics, you can't be sure if the array won't get modified underneath you, and the copy won't get detached from the original reference.

我见过的最有见地的解释(帮助我得到了我的引用)在评论PHP的传递通过引用'manual:

The most insightful explanation I've seen (which helped me get my head around references) was in a comment on the PHP 'passing by reference' manual:

http: //ca.php.net/manual/en/language.references.pass.php#99549

基本上逻辑就是这样。你将如何编写自己的版本的 call_user_func()? - 然后解释如何断开与引用,以及如何失败,当你避免调用时间通过引用。换句话说,当你使用<$ c $时,调用函数(指定值,让PHP从函数声明中决定是否传递值或引用)的方法是不工作的c> call_user_func() - 你调用两个函数深,第一个按值,第二个参考第一个值。

Basically the logic goes like this. How would you write your own version of call_user_func() ? - and then explain how that breaks with references, and how it fails when you avoid call-time pass-by-reference. In other words, the right way to call functions (specify the value, and let PHP decide from the function declaration whether to pass value or reference) isn't going to work when you use call_user_func() - you're calling two functions deep, the first by value, and the second by reference to the values in the first.

围绕这一点,你将更深入地了解PHP引用(如果可以的话,还有更大的动机去指导清楚)。

Get your head around this, and you'll have a much deeper understanding of PHP references (and a much greater motivation to steer clear if you can).

这篇关于为什么PHP的call_user_func()函数不支持通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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