PHP 中通过引用返回
我尝试过谷歌搜索,尝试过 PHP 文档,在 Stack Overflow 上搜索答案,但找不到任何令人满意的东西。我正在读一本书,其中作者使用了按引用返回,但从未解释它是什么。作者使用的代码是
function &getSchool() {
return $this->school;
}
有人能用简单的话解释这个概念的例子吗?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您有此类:
该类有一个私有属性和两个允许您访问它的方法。一种按值返回(默认行为),另一种按引用返回。两者之间的区别在于:
Fruit
的私有属性内,因为您实际上修改的是财产的价值。Fruit::$color
的别名 - 您引用的不同名称到同一个变量。因此,如果您对它执行任何操作(包括修改其内容),您实际上是直接对属性的值执行相同的操作。下面是一些测试代码:
查看实际操作。
警告:我觉得有必要提及,虽然引用确实有合法用途,但它们是应该很少使用的功能之一,并且只有在您首先仔细考虑了任何替代方案的情况下才应使用。经验不足的程序员往往会过度使用引用,因为他们认为引用可以帮助他们解决特定问题,但同时却看不到使用引用的缺点(作为一项高级功能,其细微差别远非显而易见)。
Suppose you have this class:
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
because you are actually modifying a copy of the property's value.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:
See it in action.
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 通过引用返回。如上所述,您只需放置一个
&
来告诉 PHP 您想要这个,如果您不放置它并尝试执行$var =& somefunction()
你会得到错误“只有变量应该通过引用分配。”为了消除乔恩回答中的一些困惑。实际上没有必要有两个单独的函数,一个按引用返回,一个按值返回;在某些项目惯例之外。
&
仅允许它通过引用返回,它不会强制它通过引用返回。例如。相同的函数用于引用和非引用赋值
输出
引用很棒,但是如果您不熟悉它们,请遵循以下规则:
)显然是非常功利的用途(即。当您实际上想要共享一个值以简化操作它的代码时)。
再说一遍,永远不要引用对象(这是毫无意义的!)
Easy rule: if you place an
&
in front of a function name in PHP you can use&
when you call the function (ie. it's return value) as if it was a variable and not a function.That's it.
It's easier to think of it as "PHP allowing you to reference the return value" rather then PHP returning by reference. As mentioned above you simply place an
&
to tell PHP you want this, if you don't place it and try do to$var =& somefunction()
you will get the error "Only variables should be assigned by reference."To clear up some confusion with Jon's answer. There is actually no need to have two separate functions one returning by reference and one returning by value; outside of some project convention. The
&
only allows it to return by reference, it doesn't force it to return by reference.eg. same function used both for reference and non-reference assignments
Output
References are great however if you're not familiar with them please follow the following rules:
Exceptions would obviously be very utilitarian uses (ie. when you actually want to share a value to simplify code for manipulating it).
Again, NEVER REFERENCE OBJECTS (it's pointless!)
您发布的示例可能来自 PHP4 或更早版本。不再需要通过引用返回对象,因为在 PHP5+ 中它们几乎总是自动以这种方式完成。
请参阅返回 PHP 文档中的引用
The example you posted is probably from PHP4 or earlier. It's no longer necessary to return objects by reference as they are almost always done that way automatically in PHP5+.
See return references in PHP docs