Return by reference in PHP(在 PHP 中通过引用返回)
问题描述
我尝试了谷歌搜索,尝试了 PHP 文档,搜索了 Stack Overflow 的答案,但找不到任何令人满意的东西.我正在读一本书,其中作者使用了Return by Reference,但从未解释过它是什么.作者使用的代码是
I tried Googling, tried PHP Documentation, searched Stack Overflow for an answer but couldn't find anything satisfactory. I was reading a book in which author have made use of Return by Reference but never explained what it is. The code used by the author is
function &getSchool() {
return $this->school;
}
有人可以用简单的话解释一下这个概念吗?
Can someone explain in simple words with an example about this concept.
推荐答案
假设你有这个类:
class Fruit {
private $color = "red";
public function getColor() {
return $this->color;
}
public function &getColorByRef() {
return $this->color;
}
}
该类有一个私有属性和两个允许您访问它的方法.一个按值返回(默认行为),另一个按引用返回.两者的区别在于:
The class has a private property and two methods that let you access it. One returns by value (default behavior) and the other by reference. The difference between the two is that:
- 使用第一种方法时,您可以对返回的值进行更改,这些更改不会反映在
Fruit
的私有属性中,因为您实际上是在修改属性值的副本. - 使用第二种方法时,您实际上是在返回
Fruit::$color
的别名 -- 不同的名称您指的是相同的变量.因此,如果您对它进行任何操作(包括修改其内容),您实际上是在直接对属性的值执行相同的操作.
- When using the first method, you can make changes to the returned value and those changes will not be reflected inside the private property of
Fruit
because you are actually modifying a copy of the property's value. - When using the second method, you are in fact getting back an alias for
Fruit::$color
-- a different name by which you refer to the same variable. So if you do anything with it (including modifying its contents) you are in fact directly performing the same action on the value of the property.
这里有一些代码来测试它:
Here's some code to test it:
echo "
TEST RUN 1:
";
$fruit = new Fruit;
$color = $fruit->getColor();
echo "Fruit's color is $color
";
$color = "green"; // does nothing, but bear with me
$color = $fruit->getColor();
echo "Fruit's color is $color
";
echo "
TEST RUN 2:
";
$fruit = new Fruit;
$color = &$fruit->getColorByRef(); // also need to put & here
echo "Fruit's color is $color
";
$color = "green"; // now this changes the actual property of $fruit
$color = $fruit->getColor();
echo "Fruit's color is $color
";
查看实际操作.
警告:我觉得有必要提一下,虽然它们确实有合法用途,但它们是那些应该很少使用的功能之一,只有在你仔细考虑过任何替代品的情况下才能使用首先.经验不足的程序员倾向于过度使用引用,因为他们认为可以帮助他们解决特定问题,同时又不会看到使用引用的缺点(作为一项高级功能,其细微差别远非显而易见).
Warning: I feel obliged to mention that references, while they do have legitimate uses, are one of those features that should be used only rarely and only if you have carefully considered any alternatives first. Less experienced programmers tend to overuse references because they see that they can help them solve a particular problem without at the same time seeing the disadvantages of using references (as an advanced feature, its nuances are far from obvious).
这篇关于在 PHP 中通过引用返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中通过引用返回
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01