title: Smarty (and other tpl ngins): assign and assign_by_ref
body: <p>This is not just about Smarty, but I guess most template engines that have variables assigned. It's more a theoretical question, than a practical. I have no use case.</p>
  
  <p>What happens in PHP when you assign a big array <code>$a</code> to another variable <code>$b</code>? PHP copies the array? Maybe, just maybe, internally it creates a pointer. Then what happens when you alter <code>$a</code> slightly? <code>$b</code> shouldn't be changed, because no <code>&amp;</code> was used to create <code>$b</code>. Did PHP just double the memory usage??</p>
  
  <p>More specifically: What happens when you assign a big array from you Controller (<code>$a</code>) to your template engine (<code>$tpl-&gt;vars['a']</code>) and to use in the view (<code>extract</code> to <code>$a</code>)? Did PHP's memory just triple??</p>
  
  <p>Now what happens if I assign all my variables by reference? I'm cool with my view being able to alter the array back into the Controller (I won't be coming back there anyway). It's also fine if the variable changes within the templat engine (<code>$tpl-&gt;vars['a']</code>).</p>
  
  <p>Is assigning all vars by reference better for memory? Better for performance? If so: any chances of strange, unwanted side effects?</p>
  
  <p>Because people like code and not stories:</p>
  
  <pre><code>// copies
  $a = array( ... );
  $tpl-&gt;assign('a', $a); // creates a copy (?) in $tpl-&gt;vars['a']
  
  // pointer / by ref
  $a = array( ... );
  $tpl-&gt;assign_by_ref('a', $a); // creates a pointer in $tpl-&gt;vars['a'] because:
  
  function assign_by_ref( $name, &amp;$var ) {
    $this-&gt;vars[$name] = $var; // voila pointer?
  }
  </code></pre>
  
  <p>I'm pretty sure PHP doesn't mind big arrays and copies and clones, but performance and memory wise: which's 'better'?</p>
  
  <p><strong>edit</strong><br>
  For objects, all of this doesn't matter. Objects are always, automatically assigned by reference. And since objects are hot, maybe this is an outdated question, but I <strong>am</strong> very curious.</p>
  
  <p><strong>UPDATE</strong><br>
  So PHP uses <code>copy on write</code>... Love it. And objects are always pointers. What happens when you:</p>
  
  <pre><code>$a = new BigObject;
  $b = $a; // pointer, right?
  $b-&gt;updateSomethingInternally(); // $b is now changed &gt; what about $a?
  </code></pre>
  
  <p>Did this trigger the copy-on-write? Or are $a and $b still identical (like in <code>===</code>)?</p>
  
  <p><strong>edit</strong><br>
  Could I conclude that assigning by ref is really not worth it just to spare memory? PHP in itself is smart enough?</p>
  
  <p><strong>edit</strong><br>
  Interesting visualization of copy, clone, by-ref etc: <a href="http://www.phpinsider.com/download/PHP5RefsExplained.pdf" rel="nofollow">http://www.phpinsider.com/download/PHP5RefsExplained.pdf</a></p>