在 PHP 中按引用返回 [英] Return by reference in PHP

查看:16
本文介绍了在 PHP 中按引用返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了谷歌搜索,尝试了 PHP 文档,在 Stack Overflow 上搜索了答案,但没有找到任何令人满意的答案.我正在读一本书,其中作者使用了引用返回但从未解释过它是什么.作者使用的代码是

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 中按引用返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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