title: Unsetting variable at reference in php
body: <p>I have a class which contains an array of objects and has methods to return an object from that array by reference. There is also a method to unset an object from the array.</p>
  
  <p>However if I have a variable that references an object from the array, and that element is unset, the variable still has a reference to it. What do I need to do in the remove method that will destroy that object for good, including references to it.</p>
  
  <pre><code>class myClass
  {
      public $objectList = array();
  
      public function __construct()
      {
          $objectList[] = new myObject();
      }
  
      public function &amp;getObjectByReference()
      {
           return $this-&gt;objectList[0];
      }
  
      public function removeObject()
      {
          unset($this-&gt;objectList[0]);
      }
  }
  $myClass = new myClass();
  $referencedObject = $myClass-&gt;getObjectByReference(); 
  // this can now use the methods from myObject
  $myClass-&gt; removeObject(); 
  // supposed to delete the object from memory. However $referencedObject is still
  // able to use all the methods from myObject.
  </code></pre>
  
  <p>So thats the problem I am having, I need to be able to remove from the array and delete the object from memory so variables that reference that object are no longer usable.</p>