2

I am very confused about how using & operator to reduce memories.

Can I have an answer for below question??

clase C{

  function B(&$a){
       $this->a = &$a; 

       $this->a = $a;

       //They are the same here created new variable $this->a??
       // Or only $this->a = $a will create new variable?  
  }
} 

$a = 1;

C = new C;
C->B($a)

Or maybe my understanding is totally wrong.....

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
Micah
  • 4,254
  • 8
  • 30
  • 38

3 Answers3

12

Never ever use references in PHP just to reduce memory load. PHP handles that perfectly with its internal copy on write mechanism. Example:

$a = str_repeat('x', 100000000); // Memory used ~ 100 MB
$b = $a;                         // Memory used ~ 100 MB
$b = $b . 'x';                   // Memory used ~ 200 MB

You should only use references if you know exactly what you are doing and need them for functionality (and that's almost never, so you could as well just forget about them). PHP references are quirky and can result to some unexpected behaviour.

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
3

Value type variables will only be copied when their value changes, if you only assign it in your example it wont be copied, memory footprint will be the same as if u have not used the & operator.

I recommend that you read these articles about passing values by reference:

When to pass-by-reference in PHP

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

http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html

it is considered a microoptimalization, and hurts the transparency of the code

Community
  • 1
  • 1
VuesomeDev
  • 4,095
  • 2
  • 34
  • 44
3

I am very confused about how using & operator to reduce memories.

If you don't know it, you probably don't need it :) The & is quite useless nowadays, because of several enhancements in the PHP-core over the last years. Usually you would use & to avoid, that PHP copies the value to the memory allocated for the second variable, but instead (in short) let both variables point to the same memory.

But nowadays

  • Objects are passed as reference anyway. They don't clone themself magically, because they are passed to a method ;)
  • When you pass primitive types, PHP will not copy the value, unless you change the variable (copy-on-write).

To sum it up: The benefits of & already exists as feature of the core, but without the ugly side-effects of the operator

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • Thank you very much, so use & or not is eventually use same amount of memories? am i understand it right? sorry... – Micah Jan 24 '13 at 12:18
  • 1
    `&` is hardly useless when used for its intended purpose. I used it just yesterday for aliasing `$itemTree[$a][$b][$id]` to `$items[$id]` to simplify the hell out of looping over items. :) But the first sentence is the important one; if you don't know what you're doing with it, you almost certainly shouldn't be using it just to save memory or whatever. – cHao Jan 24 '13 at 12:29
  • @Till Premature optimization is the root of all evil ;) Do you really have such a huge memory usage, that you must take care of it, and it is not solvable by (for example) chunks/pagination? `&` is a reference, means: If you change the value of one of the variables, the other one gets changed too (because both points to the same `zval` ("memory"). You will only produce hard to track errors and you win nearly nothing. – KingCrunch Jan 24 '13 at 12:30
  • thank you very much, I asked regardless coding, for knowledge. And thank you very much for all your answer, I perfectly understand it now. – Micah Jan 24 '13 at 12:32