PHP 5.x 中的引用传递
我正在学习 Zend PHP5 认证,一切看起来都不错,但我找不到传递或返回我的参考变量的真实示例。
如果有人能举例说明何时使用它,那就太好了?
I am studying for the Zend PHP5 Certification, everything looks good but i can not find real world examples for passing or returning variables my reference.
It would be very nice, if someone has an example when to use this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您要编写一个函数来从数组中删除某些值。
您可以复制数组并返回它。但是如果您想知道是否有任何内容被删除怎么办?相反,您就地修改数组并返回删除的元素数。因此:
如果您在函数内创建一个复杂的结构,您可能希望返回对它的引用而不是它的副本。但这是一个相当边缘的情况,因为 PHP 使用写时复制(即在修改之前不会复制),但有一些有效的用例。
当您编写类的成员函数时,通过引用返回更有意义。您可以返回对数据成员的引用,但这也会破坏封装。
最后,值得注意的是,默认情况下所有对象都是通过引用传递和返回的。其他一切都按值传递和返回。
Let's say you want to write a function that removes certain values from an array.
You could copy the array and return it. But what if you want to know if anything was removed? Instead you modify the array in-place and return the number of elements removed. So:
If you create a complex structure inside your function you may want to return a reference to it rather than a copy of it. But this is a fairly marginal case because PHP uses copy-on-write (ie its not copied until it's modified) but there are some valid use cases.
Returning by reference makes more sense when you're writing a member function of a class. You could return a reference to a data member but this can also break encapsulation.
Lastly, it's worth noting that all objects are passed and returned by reference by default. Everything else is passed and returned by value.
通过引用传递:类似 asort() 的函数
Pass by reference: functions like asort()
php5中的对象默认是通过引用传递的。 (有关正确的描述,请参阅 http://docs.php.net/language.oop5.references< /a>)
通过引用传递参数已经得到解答...
作为通过引用返回值的(现实世界)示例,选择任何 流畅的接口,其中方法返回对同一对象的引用作为其自己的调用上下文($this)。如果返回值是副本/克隆,则这将不起作用(如预期)。
例如,
这会打印
foo=1 bar=2
,我认为这就是人们所期望的结果,这里并不奇怪。如果该方法按值返回 $this,则它将不再起作用。您可以通过强制 php 返回副本来测试这一点。而不是使用
return $this
行,输出将是
(我不会称该界面直观;-))
Objects in php5 are passed by reference by default. (For a correct description see http://docs.php.net/language.oop5.references)
Passing arguments by reference has already been answered...
As a (real world) example for a return value by reference pick any fluent interface where a method returns a reference to the same object as its own call-context ($this). This wouldn't work (as expected) if the return value was a copy/clone.
E.g.
This prints
foo=1 bar=2
and I think that's the result one would expect, no surprises here.If the method returned $this by value, it wouldn't work anymore. You can test that by forcing php to return a copy. Instead of the
return $this
lines useand the output will be
(and I wouldn't call that interface intuitive ;-))
如果您与 chainig 一起工作,它可能会很有用,例如:
... Sry。与 Volkers 相同:((流畅的接口就像我所知道的那样链接)。
想象一下,您有一个带有加载方法的流畅接口。加载接口替换或添加一些数据。在这里,通过调用进行操作可能很有用参考值$success判断加载成功
:
采取一些具有行列表的数据库对象。 Nr。行数指向所有对象的右侧数据(parallesl)。如果您返回 nr。并将其传递给下一个对象,您可以跳过一个对象中的行并跳过其他对象。总是有一堆有效的对象。
It could be useful, if you work with chainig,like:
... Sry. Same as Volkers :( (The Fluent Interface is Chaining like i know it).
Image you have a fluent Interface with an load-method. The Load-interface replace or adds some data. Here it could be useful to act with an call by reference value $success to determine that the load was successful.
Return by reference:
Take some database-objects having a list of rows. The Nr. of rows points to right data of all objects (parallesl). If you return the nr. and pass it to the next object, you can skip the row in one object and skip in the others too. Always having a valide bunch of objects.