When is it good to use pass by reference in PHP?

。_饼干妹妹 提交于 2019-12-17 10:21:51

问题


In C++ if you pass a large array to a function, you need to pass it by reference, so that it is not copied to the new function wasting memory. If you don't want it modified you pass it by const reference.

Can anyone verify that passing by reference will save me memory in PHP as well. I know PHP does not use addresses for references like C++ that is why I'm slightly uncertain. That is the question.


回答1:


The following does not apply to objects, as it has been already stated here. Passing arrays and scalar values by reference will only save you memory if you plan on modifying the passed value, because PHP uses a copy-on-change (aka copy-on-write) policy. For example:

# $array will not be copied, because it is not modified.
function foo($array) {
    echo $array[0];
}

# $array will be copied, because it is modified.
function bar($array) {
    $array[0] += 1;
    echo $array[0] + $array[1];
}

# This is how bar shoudl've been implemented in the first place.
function baz($array) {
    $temp = $array[0] + 1;
    echo $temp + $array[1];
}


# This would also work (passing the array by reference), but has a serious 
#side-effect which you may not want, but $array is not copied here.
function foobar(&$array) {
    $array[0] += 1;
    echo $array[0] + $array[1];
}

To summarize:

  • If you are working on a very large array and plan on modifying it inside a function, you actually should use a reference to prevent it from getting copied, which can seriously decrease performance or even exhaust your memory limit.

  • If it is avoidable though (that is small arrays or scalar values), I'd always use functional-style approach with no side-effects, because as soon as you pass something by reference, you can never be sure what passed variable may hold after the function call, which sometimes can lead to nasty and hard-to-find bugs.

  • IMHO scalar values should never be passed by reference, because the performance impact can not be that big as to justify the loss of transparency in your code.




回答2:


The short answer is use references when you need the functionality that they provide. Don't think of them in terms of memory usage or speed. Pass by reference is always going to be slower if the variable is read only.

Everything is passed by value, including objects. However, it's the handle of the object that is passed, so people often mistakenly call it by-reference because it looks like that.

Then what functionality does it provide? It gives you the ability to modify the variable in the calling scope:

class Bar {}
$bar = new Bar();

function by_val($o) { $o = null; }
function by_ref(&$o) { $o = null; }

by_val($bar); // $bar is still non null
by_ref($bar); // $bar is now null

So if you need such functionality (most often you do not), then use a reference. Otherwise, just pass by value.

Functions that look like this:

$foo = modify_me($foo);

sometimes are good candidates for pass-by-reference, but it should be absolutely clear that the function modifies the passed in variable. (And if such a function is useful, often it's because it really ought to just be part of some class modifying its own private data.)




回答3:


In PHP :

  • objects are passed by reference1 -- always
  • arrays and scalars are passed by value by default ; and can be passed by reference, using an & in the function's declaration.


For the performance part of your question, PHP doesn't deal with that the same way as C/C++ ; you should read the following article : Do not use PHP references


1. Or that's what we usually say -- even if it's not "completely true" -- see Objects and references



来源:https://stackoverflow.com/questions/5479073/when-is-it-good-to-use-pass-by-reference-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!