Returning by reference

前端 未结 1 1939
失恋的感觉
失恋的感觉 2021-01-18 19:53

In the PHP documentation it says:

Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own.

相关标签:
1条回答
  • 2021-01-18 20:26

    PHP uses copy on write. That means that if you pass the huge array as a function parameter and only read from it (such as a foreach), you won't be writing to it so it doesn't need to make a copy.

    $count = count($huge_array); // read the reference at the bottom
    for($i = 0; $i < $count $i++) {
        $value = 2*$huge_array[$i]/15;
        if($value > 3)
            $sub_array []= $value;
    }
    

    The $subarray which should be smaller (it is a subset of the huge array), will contain only the needed (changed) data.

    If you do not intend on changing the values of the original $huge_array it will never get copied so no extra memory is used.

    If you intend on changing the original values of the array, you need to pass it by reference.

    If you intend on making and returning an altered version of the original array then you do need the extra memory PHP is allocating for you.

    If you intend on making and returning an altered smaller subset of the original array then you will create a new empty array into which you will copy some of data from the huge array and should be careful not to overwrite values in the $huge_array, so you'll avoid writing from $huge_array and emphasise on reading from it.

    This link explains that PHP was optimised for pass by value use cases.

    Copy on write only works if the variable isn't a reference being passed to a function expecting a value, if it is, passing it around triggers a copy.

    That makes PHP native functions that expected an argument to be passed by value and received a referenced variable copy the value of the reference.

    function foo(&$data) {
        for ($i = 0; $i < strlen($data); $i++) {
            do_something($data{$i});
        }
    }
    
    $string = "... looooong string with lots of data .....";
    foo(string);
    

    Executing strlen in C would imply iterating over the entire string to count it. In PHP strings have an attached length value. So strlen returns the value read from there (fast).

    But if you give it a referenced variable it will have to copy it before reading it's length so it will iterate over the value to copy it into it's argument list, read and return the length (and subsequently release the memory for the freshly copied string).

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