title: Does PHP support the RAII pattern? How?
body: <p>Most resources on PHP never touch memory management because the language itself is pretty good at doing this for you. However, in PHP you often end up dealing with external resources which aren't memory -- database handles, sessions, database transactions, etc. These external resources could be managed most cleanly using some form of RAII object.</p>
  
  <p>I initially thought that PHP used a garbage collection scheme similar to the JVM or the CLR, where the concept of a destructor does not exist. (Remember: <a href="http://blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx" rel="noreferrer">Everyone thinks about garbage collection the wrong way</a> -- finalizers are not destructors!) There's the special <code>__destruct</code> method, but I thought that was a "finalizer" similar to a Java or C# finalizer. For this reason, you cannot use RAII on the JVM or the CLR (C#'s <code>using</code> blocks get you about 95% of the way there, but that's a bit different...).</p>
  
  <p>However, <a href="https://www.google.com/search?gcx=c&amp;ix=c2&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=php+raii" rel="noreferrer">Google seems to indicate that PHP supports the RAII pattern</a>, though I cannot find verification of this in the PHP docs. Does the language support this and is putting the cleanup logic in <code>__destruct</code> sufficient for accomplishing RAII tasks?</p>