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

前端 未结 3 1962
悲&欢浪女
悲&欢浪女 2020-12-02 09:53

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 modifie

相关标签:
3条回答
  • 2020-12-02 10:23

    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.)

    0 讨论(0)
  • 2020-12-02 10:33

    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

    0 讨论(0)
  • 2020-12-02 10:42

    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.

    0 讨论(0)
提交回复
热议问题