title: Reference to class persisting in loop
body: <p>I create a bunch of instances of a basic class via a loop.
  Each iteration, I add (by reference, not copy) the instance to an array.</p>
  
  <p><strong>Why is it that after the loop, every reference in the array is to the last instance created?</strong></p>
  
  <p>Doing an unset seems to fix the issue, however I don't believe this is ideal and could potentially unset the underlying instance from memory.</p>
  
  <pre><code>&lt;?php
  //foobars remembers something
  class FOOBAR{
      public $val;
      public function __construct(&amp;$input){
          $this-&gt;val = $input;
      }
  };
  
  //after creating foobars, pass them to a list
  $list1 = [];
  for($i=1; $i&lt;=5; $i++){
      //create an instance of foobar
      $random = rand(1, 10);
      $instance = new FOOBAR($random);
      $list1[] = &amp;$instance;
  
      // Using unset (below) fixes it?
      //unset($instance);
  }
  
  //show what our foobars remembered
  var_dump(json_encode($list1));
  ?&gt;
  </code></pre>