return large data by reference or as return in function?

前端 未结 1 1819
名媛妹妹
名媛妹妹 2021-02-08 06:48

On the job today I had an arguement with a collage about passing large data between scopes. The myth was that reference uses less memory/CPU usage when passing between 2 scopes.

1条回答
  •  误落风尘
    2021-02-08 07:39

    This is due to the way php handles variables, and is a bit counter-intuitive to anyone who has worked in C or C++.

    Passing by reference to be smarter than PHP isn't advised. PHP doesn't actually make copies of data unless it needs to (i.e. you change a variable's value when there's more than 1 reference to it), an optimization strategy very similar to copy-on-write for shared memory pages.

    So, let's say you have a variable that you pass by value several times in a given script. If you then take this variable and pass it by reference, you're actually duplicating the variable rather than just getting a pointer to the object.

    This is because internally, PHP zvals (the data structure PHP uses to store variables) can only be reference variables or non-reference variables. So it doesn't matter what the zval's ref_count field is, because it's not a reference variable (the is_ref field of the zval structure). So internally, PHP is forced to create a new zval and set its is_ref field to true, thus doubling the memory.

    Tell your co-worker to stop trying to outsmart PHP. Passing by reference unless done 100% perfectly throughout the code will cause a lot of overhead and double the memory usage.

    For a more detailed discussion, please see this link: http://porteightyeight.com/2008/03/18/the-truth-about-php-variables/

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