PHP 函数用 & 符号开头是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1676897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean to start a PHP function with an ampersand?
提问by Alex Mcp
I'm using the Facebook library with this code in it:
我正在使用包含以下代码的 Facebook 库:
class FacebookRestClient {
...
public function &users_hasAppPermission($ext_perm, $uid=null) {
return $this->call_method('facebook.users.hasAppPermission',
array('ext_perm' => $ext_perm, 'uid' => $uid));
}
...
}
What does the & at the beginning of the function definition mean, and how do I go about using a library like this (in a simple example)
函数定义开头的 & 是什么意思,我该如何使用这样的库(在一个简单的例子中)
回答by Dominic Rodger
An ampersand before a function name means the function will return a reference to a variable instead of the value.
函数名前的 & 号表示函数将返回对变量的引用而不是值。
Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.
当您想使用函数查找引用应该绑定到哪个变量时,按引用返回很有用。不要使用按引用返回来提高性能。引擎将自动对此进行优化。仅在您有有效的技术原因时才返回引用。
See Returning References.
请参阅返回引用。
回答by troelskn
It's returning a reference, as mentioned already. In PHP 4, objects were assigned by value, just like any other value. This is highly unintuitive and contrary to how most other languages works.
如前所述,它正在返回一个引用。在 PHP 4 中,对象是按值分配的,就像任何其他值一样。这是非常不直观的,并且与大多数其他语言的工作方式相反。
To get around the problem, references were used for variables that pointed to objects. In PHP 5, references are very rarely used. I'm guessing this is legacy code or code trying to preserve backwards compatibility with PHP 4.
为了解决这个问题,引用被用于指向对象的变量。在 PHP 5 中,很少使用引用。我猜这是遗留代码或试图保留与 PHP 4 的向后兼容性的代码。
回答by Elisha Senoo
This is often known in PHP as Returning referenceor Returning by reference.
这在 PHP 中通常称为Returning reference或Returning by reference。
Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so.
当您想使用函数查找引用应该绑定到哪个变量时,按引用返回很有用。不要使用按引用返回来提高性能。引擎将自动对此进行优化。仅在您有有效的技术原因时才返回引用。
PHP documentation on Returning reference
A referencein PHP is simply another name assigned to the content of a variable. PHP references are not like pointers in C programming, they are not actual memory addresses, so they cannot be used for pointer arithmetics.
PHP 中的引用只是分配给变量内容的另一个名称。PHP 引用不像 C 编程中的指针,它们不是实际的内存地址,因此它们不能用于指针运算。
The concept of returning references can be very confusing especially to beginners, so an example will be helpful.
返回引用的概念可能非常令人困惑,尤其是对于初学者而言,因此示例会有所帮助。
$populationCount = 120;
function &getPopulationCount() {
global $populationCount;
return $populationCount;
}
$countryPopulation =& getPopulationCount();
$countryPopulation++;
echo "$populationCount = $populationCount\n"; // Output: $populationCount = 121
echo "$countryPopulation = $countryPopulation\n"; //Output: $countryPopulation = 121
The function getPopulationCount()defined with a preceding &, returns the reference to the content or value of $populationCount. So, incrementing $countryPopulation, also increments $populationCount.
getPopulationCount()用前面定义的函数&,返回对内容或值的引用$populationCount。所以,递增$countryPopulation,也递增$populationCount。

