title: PHP Storing object reference in an array
body: <p>I'm having some trouble with references in PHP. Let's say you do the following:</p>
  
  <pre><code>&lt;?php
  
  class foo
  {
      public $str;
  }
  
  class bar
  {
      public $txt;
  }
  
  class x
  {
      private $objs = array();
  
      public function add(&amp;$obj)
      {
          $this-&gt;objs[] = $obj;
      }
  
      public function update()
      {
          foreach($this-&gt;objs as $obj)
          {
              $obj = new bar();
              $obj-&gt;txt = "bar";
          }
       }
  }
  
  //Make x
  $x = new x();
  
  //Make foo
  $foo = new foo();
  $foo-&gt;str = "foo";
  
  //Add
  $x-&gt;add($foo);
  
  //update
  $x-&gt;update();
  
  var_dump($foo);
  </code></pre>
  
  <p>The <code>var_dump</code> at the end gives:</p>
  
  <pre><code>class foo#2 (1) {
    public $str =&gt;
    string(3) "foo"
  }
  </code></pre>
  
  <p>Evidently, I'm not storing my reference properly in the array. Is there a way to do this so that <code>$foo</code> is of type <code>bar</code> after <code>$x-&gt;update()</code>?</p>