title: Loading an extended class from an autoloaded class in PHP
body: <p>I tried to find a similar question or scenario, but I couldn't. I have the following structure:</p>
  
  <ul>
  <li>classes/
  <ul>
  <li>dev/
  <ul>
  <li>class1.php</li>
  <li>class2.php</li>
  </ul></li>
  <li>class1.php</li>
  <li>class2.php</li>
  <li>class3.php</li>
  </ul></li>
  </ul>
  
  <p>Then, the code calls:</p>
  
  <pre><code>$class1 = new class1();
  $class2 = new class2();
  $class3 = new class3();
  </code></pre>
  
  <p>The 'dev' folder contains classes that should overwrite the root classes. There's an extended class1 inside the 'dev' folder, class2 as well, but not class3. Currently, I'm trying to load this extended class inside the autoload function, which works, but not the way I was expecting :D</p>
  
  <p>Instead of loading class1 from the root folder, then class1 from the 'dev' folder, it loads the root classes only after leaving the autoload function. Simplifying the code, I have something like:</p>
  
  <pre><code>function $autoloader($class) {
      if (!file_exists($class)) {
          echo 'failed - fallback or do something else';
          return false;
      }
      require_once($class);
  
      // Instantiates extended class from 'dev'
      if (file_exists($classFromDevFolder)) {
          require_once($classFromDevFolder);
          $newClass = new $classFromDevFolder();
          unset($newClass);
      }            
  }
  </code></pre>
  
  <p>It works, loads the class from the 'dev' folder, and extends class1 finely. But, as said, it instantiates the dev class BEFORE the root class. I assume because it's loading the dev class inside the autoload function, even after calling the root class.</p>
  
  <p>Would you guys have any ideas on how to achieve it? I also thought to create another function to load 'dev' classes, but I have no idea where to put it in order to make this function work on any class.</p>
  
  <hr>
  
  <p>I added this very simplified code sample to illustrate and clarify a bit better what I'd like to achieve.</p>
  
  <p>Root folder:</p>
  
  <pre><code>class Class1() {
      ...
      public function __construct() {
          $this-&gt;x = 1;
      }
      ...
  }
  </code></pre>
  
  <p>Dev folder:</p>
  
  <pre><code>class Class1_Dev extends Class1() {
      ...
      public function __construct() {
          $this-&gt;x = 2;
      }
      ...
  }
  </code></pre>
  
  <p>Autoloader:</p>
  
  <pre><code>function autoloader($class) {
      if (!file_exists($class)) {
          echo 'failed - fallback or something else';
          return false;
      }
      require_once($class);
      ...
      $devClass = 'dev/' . $class;
      if (file_exists($devClass)) {
          require_once($devClass);
          $newClassName = $class . '_Dev';
  
          // Here is the issue: it instantiates Class1_Dev before Class1,
          // and ultimately, Class1 keeps 'x' as 1, not 2
          $newClass = new $newClassName();
      }
  }
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>$class1 = new Class1();
  var_dump($class1-&gt;x);
  </code></pre>
  
  <p>It's expected $class->x to be 2, and not 1.</p>