title: Finding subclasses automatically in PHP
body: <p>What I am trying to do is find all subclasses that extends a certain base class.</p>
  
  <p>This is quite similar to <a href="https://stackoverflow.com/a/436204/1369417">https://stackoverflow.com/a/436204/1369417</a>, except that the classes will <em>not</em> be loaded at the time that the function is called.</p>
  
  <p>Essentially, what I am hoping to do is:</p>
  
  <ol>
  <li>Include all PHP files in a directory.</li>
  <li>Test if each include introduced a new subclass of the base class.</li>
  </ol>
  
  <p>But I want to do the include in an isolated PHP environment such that I am protected from any errors and warnings that may be introduced by including the PHP file.</p>
  
  <p>Here is a small example:</p>
  
  <p><strong>BaseClass.php</strong></p>
  
  <pre><code>&lt;?php
  abstract class BaseClass {}
  ?&gt;
  </code></pre>
  
  <p><strong>FirstSubclass.php</strong></p>
  
  <pre><code>&lt;?php
  class FirstSubclass extends BaseClass {}
  ?&gt;
  </code></pre>
  
  <p><strong>Blah.php</strong></p>
  
  <pre><code>&lt;?php
  class SecondSubclass extends BaseClass {}
  ?&gt;
  </code></pre>
  
  <p><strong>badcode.php</strong></p>
  
  <pre><code>&lt;?php
  adfgklmdflgksdfge@#4234t0q3
  ?&gt;
  </code></pre>
  
  <p><strong>main.php</strong></p>
  
  <pre><code>&lt;?php
  require('BaseClass.php');
  print_r(find_subclasses(BaseClass));
  ?&gt;
  </code></pre>
  
  <p><strong>Expected output</strong></p>
  
  <pre><code>Array (
      [FirstSubclass.php] =&gt; FirstSubclass,
      [Blah.php] =&gt; SecondSubclass,
  )
  </code></pre>