问题描述
There is a comment on an answer on SO:
答案代码:
$larry = & $fred;
评论:
这已被弃用并生成警告
但我认为这不正确.或者是吗?
But I don't think it is correct. Or is it?
所以基本上问题是:
我可以通过引用将一个变量复制到另一个变量吗?
can I copy a variable to another variable by reference?
推荐答案
取决于变量 $fred
是什么类型.如果它是一个对象, 一个指向该对象的指针(感谢 NikiC)从 PHP 5 开始无论如何都将作为值传递,因此无需显式执行此操作.到目前为止,评论是正确的.
Depends on what type variable $fred
is. If it's an object, a pointer to the object (thanks NikiC) will be passed as value anyway as of PHP 5, so there is no need to explicitly do it. Thus far, the comment is correct-ish.
然而,对于所有其他变量类型,需要明确指定按引用传递,并且不建议使用.尽管有人会说在 PHP 中使用大量引用通常不是好的编程习惯.
For all other variable types, however, passing by reference needs to be explicitly specified and is not deprecated. Although it could be argued that it's usually not good programming practice to use a lot of references in PHP.
这篇关于不推荐通过引用传递变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!