我是否需要在 PHP 5.5.X 及更高版本中使用 & 号? [英] Do I need to use the ampersand in PHP 5.5.X and above anymore?

查看:56
本文介绍了我是否需要在 PHP 5.5.X 及更高版本中使用 & 号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处都收到混合信号.

I'm getting mixed signals all over the place.

我是否使用 & 符号通过引用传递变量?

Do I use the ampersand to pass variables by reference or not?

以下链接似乎告诉我它已被弃用,不再需要:
http://gtk.php.net/manual/zh/html/tutorials/tutorials.changes.references.html

The following link seems to tell me that it is deprecated and no longer necessary:
http://gtk.php.net/manual/en/html/tutorials/tutorials.changes.references.html

但是像这样的话题让我想知道:
不推荐使用调用时传递引用?
PHP 和与符号

But threads like these make me wonder:
Call-time pass-by-reference deprecated?
PHP and ampersand

让我以我的问题为例.

如果我创建一个函数,使用 PHP 5.5.5:

If I make a function, using PHP 5.5.5:

function recurring_mailer_form($form, $form_state) 
{

}

是不是一样:

function recurring_mailer_form($form, &$form_state) 
{

}

?

推荐答案

不同的文章似乎在说不同的事情的原因是它们在谈论不同类型的传递引用.

The reason different articles seem to be saying different things is that they are talking about different kinds of pass-by-reference.

决定一个参数是否应该通过引用传递的主要因素是函数签名本身,自 PHP 4 以来,它的基本原理没有改变.考虑这个例子:

The main thing that determines if a parameter should be passed by reference is the function signature itself, and the fundamentals of this have not changed since PHP 4. Consider this example:

function foo( $by_value, &$by_reference ) { /* ... */ }

$a = 1; $b = 2;
foo( $a, $b );

这里,外部变量 $a 是按值传递给函数的,就好像它被赋值为 $by_value = $a; - 更改为 $by_value 不能影响 $a.然而,变量$b 是通过引用 传递的;就像表单 $by_reference =& 的赋值一样$b; 这意味着有两个名称引用了一个变量,对一个变量的任何赋值都将作为对两者的赋值.

Here, the outer variable $a is being passed to the function by value, as though it is being assigned as $by_value = $a; - changes to $by_value cannot affect $a. The variable $b however is being passed by reference; just like an assignment of the form $by_reference =& $b; this means that there is one variable referenced by two names, and any assignment to one will act as an assignment to both.

如果您按值传递普通"值(字符串、数字或数组),则其值只会复制到新变量中.自 PHP 5 起:但是,如果您按值传递对象,则会发生一些稍微不同的情况 - 复制的值"只是指向同一对象的指针.这意味着如果 $a 是一个对象,你可以调用 $by_value->some_property = 42;$a->some_property也将是 42.但是,如果您为 $by_value 分配了一些新值,它仍然不会影响 $a.

If you pass an "ordinary" value (a string, number, or array) by value, its value is just copied to the new variable. As of PHP 5: If you pass an object by value, however, something slightly different happens - the "value" copied is just a pointer to the same object. This means that if $a were an object, you could call $by_value->some_property = 42; and $a->some_property would also be 42. However, if you assigned some new value to $by_value, it would still not affect $a.

直到 PHP 5.4,有一种额外方法可以通过引用传递参数,即在调用时强制"引用行为/em>.这意味着您可以编写 foo(&$a, &$b); 并在 foo() 中捕获"对 $by_value 所做的更改 函数.依靠这个通常是一个坏主意,所以它被删除了.(它在 5.4 中登陆,因为它打算在 PHP 6 中删除,但该项目被无限期搁置,较小的更改登陆 5.3 和 5.4).

Until PHP 5.4, there was an extra way to pass a parameter by reference, which was to "force" the reference behaviour at call-time. This meant that you could write foo(&$a, &$b); and "capture" changes made to $by_value inside the foo() function. Relying on this was generally a bad idea, and so it was removed. (It landed in 5.4 because it was intended for removal in PHP 6, but that project was put on indefinite hold, with the smaller changes landing in 5.3 and 5.4).

最后,函数可以通过引用返回变量(如在此处的手册中讨论的那样).这有点繁琐,因为它实际上要求您将 & 放在 两个 位置:在函数声明的开头,表示 return 应该意味着返回这个变量引用"而不是返回这个值";并在调用它的代码中,为该引用分配一个变量,而不仅仅是复制其值.这是一个愚蠢的例子,它结合了一个引用参数和一个引用返回(两者不必放在一起,这只是一个例子):

Finally, functions can return a variable by reference (as discussed in the manual here). This is a little fiddly, as it actually requires you to put & in two places: at the beginning of the function declaration, to say that return should mean "return this variable reference" not "return this value"; and in the code calling it, to assign a variable to that reference, rather than just copying its value. Here's a silly example which combines a reference parameter with a reference return (the two do not have to go together, it's just an example):

function &bar(&$some_param) { return $some_param; }
$a = 1;
$b =& bar($a);
// $b and $a now point at the same variable, not just the same value
// it was passed into and out of a function, and assigned to a new variable, 
// but all those operations were by reference

请注意,许多人错误地认为通过引用传递变量会给他们带来性能优势,而这通常是他们使用调用时传递引用的唯一原因.这实际上通常是错误的,因为支持 PHP 的 Zend 引擎使用一种称为写时复制"的技术来留下多个变量,这些变量恰好具有指向同一块内存的相同值,即使它们没有绑定为参考.事实上,由于引擎跟踪哪些变量处于写时复制状态的方式,引用分配通常打败这种优化.

Note that many people mistakenly believe that passing a variable by reference will give them a performance benefit, and this was often their only reason for using call-time pass-by-reference. This is in fact usually wrong, as the Zend Engine which powers PHP uses a technique called "copy on write" to leave multiple variables which happen to have the same value pointing at the same piece of memory, even if they are not bound up as references. In fact, reference assignment generally defeats this optimisation, due to the way the engine tracks which variables are in the copy-on-write state.

这篇关于我是否需要在 PHP 5.5.X 及更高版本中使用 & 号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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