title: When (at which moment) is __destruct called?
body: <p>Is there some assurance when __destruct is called, when considering simple objects without reference cycles? I know from java, that it is not defined when and if the function finalize is called, it depends on the garbage collector.</p>
  
  <p>For example if you have a function like:</p>
  
  <pre><code>1   function test(){
  2       $x = new SomeObject();
  3       $y = new SomeObject();
  4       $x = null;
  5   }
  </code></pre>
  
  <p>Where <code>SomeObject</code> has no reference cycles.</p>
  
  <p>Can you assume <code>$x-&gt;__destruct</code> is called at line 4 and <code>$y-&gt;__destruct</code> is called at line 5?</p>
  
  <p>Testing the following script seems to indicate that this is true:
  <a href="https://gist.github.com/KAYLukas/0b1d65e57b52862f8da5" rel="nofollow">https://gist.github.com/KAYLukas/0b1d65e57b52862f8da5</a></p>
  
  <p>Furthermore, <code>SplFileObject</code> seems to work on this predicate: it is impossible to close the file, instead you just need to set the variable to null. If this would not call <code>__destruct</code> directly it would be impossible to open the file reliable after it has been opened with <code>SplFileObject</code>.</p>
  
  <p>I have found that there is an assurance that the <code>__destruct</code> will be called eventually, just not when.</p>