简体   繁体   English

在PHP中通过引用自动返回

[英]Automatic Return By Reference in PHP

For some reason I have always assumed that most of the time a variable returned from a method would be returned by reference - after all on return; 出于某种原因,我总是假设大多数时候从方法返回的变量将通过引用返回 - 毕竟返回时; most methods would destroy the return value and it seems silly to make a copy, return it, then destroy the originals. 大多数方法会破坏返回值,制作副本,返回它,然后销毁原件似乎很愚蠢。

Does the above ever apply, or is it worth going through and making functions return by reference manually? 以上是否适用,或者是否值得通过手动返回参考函数? I have a few methods that often pass large amounts of data between themselves and if it is the case it would be a cheap way of getting some more performance out of them. 我有一些方法经常在它们之间传递大量数据,如果是这种情况,它将是一种从它们中获得更多性能的廉价方法。

Thanks for any comments! 谢谢你的评论!

PHP does 'copy on write' anyway, so variables aren't actually copied until you actually modify the value. PHP无论如何都会“复制写入”,因此在实际修改值之前,实际上不会复制变量。 So you shouldn't need to worry about this. 所以你不必担心这个。

Also from http://php.net/manual/en/language.references.return.php : 也来自http://php.net/manual/en/language.references.return.php

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. 只有在有正当技术理由的情况下才返回引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM