When to pass-by-reference in PHP

后端 未结 5 555
慢半拍i
慢半拍i 2020-11-27 04:45

Im wondering if its good practice to pass-by-reference when you are only reading a variable, or if it should always be passed as a value.

Example with pass-by-refere

5条回答
  •  清歌不尽
    2020-11-27 05:38

    PHP makes use of copy-on-write as much as possible (whenever it would typically increase performance) so using references is not going to give you any performance benefit; it will only hurt. Use references only when you really need them. From the PHP Manual:

    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.

提交回复
热议问题