title: PHP - how to destroy object and objects it contains?
body: <p>I have a CLI script written in PHP. In this script I have one instance of mainClass which contains a lot of instances of objects of other types which are stored in PHP arrays. How do I destroy $mainObject and all of the objects it contains?</p>
  
  <p>EXAMPLE CODE:</p>
  
  <pre><code>class mainClass
  {
      private $_array1 = array();
      private $_array2 = array();
      private $_array3 = array();
  
      public function __construct($data)
      {
          foreach ($data['a1'] as $val) {
              $this-&gt;_array1[] = new Object1($val);
          }
          foreach ($data['a2'] as $val) {
              $this-&gt;_array2[] = new Object2($val);
          }
          foreach ($data['a3'] as $val) {
              $this-&gt;_array3[] = new Object3($val);
          }
      }
  }
  
  $mainObject = new mainClass($data);
  
  function someFunction(mainClass $mainObject)
  {
      unset($mainObject);
  }
  </code></pre>
  
  <ol>
  <li>Does unset($mainObject) will destroy it and all objects it contains?</li>
  <li>Do I have to destroy every object separately?</li>
  <li>Should I use destructor of the mainClass to destroy objects (call theirs destructors) that $mainObject contains?</li>
  </ol>