title: PSR-4 autoloader
body: <p>I'm testing PSR-4 autoloader from <a href="https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md" rel="nofollow noreferrer">https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md</a> and something is not working.  </p>
  
  <p><strong>Error:</strong></p>
  
  <p>Fatal error: Uncaught Error: Class 'Foo\Bar\Qux\Quux' not found in C:\xampp\htdocs\Home\foo-bar\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Home\foo-bar\index.php on line 11</p>
  
  <p><strong>Line:</strong></p>
  
  <p>new \Foo\Bar\Qux\Quux;</p>
  
  <p><strong>My files:</strong> </p>
  
  <ul>
  <li>index.php</li>
  <li>example
  
  <ul>
  <li>loader.php</li>
  </ul></li>
  <li>src
  
  <ul>
  <li>Qux
  
  <ul>
  <li>Quux.php</li>
  </ul></li>
  <li>Baz.php</li>
  </ul></li>
  <li>tests
  
  <ul>
  <li>Qux
  
  <ul>
  <li>Quux.php</li>
  </ul></li>
  <li>BazTest.php</li>
  </ul></li>
  </ul>
  
  <p><strong>index.php:</strong></p>
  
  <pre><code>&lt;?php
  
  require_once "Example/loader.php";
  $loader = new \Example\Psr4AutoloaderClass;
  
  $loader-&gt;register();
  
  $loader-&gt;addNamespace('Foo\Bar', '/src');
  $loader-&gt;addNamespace('Foo\Bar', '/tests');
  
  new \Foo\Bar\Qux\Quux;
  </code></pre>
  
  <p><strong>loader.php:</strong></p>
  
  <pre><code>&lt;?php namespace Example;
  
  class Psr4AutoloaderClass
  {
      protected $prefixes = array();
  
      public function register()
      {
          spl_autoload_register(array($this, 'loadClass'));
      }
  
      public function addNamespace($prefix, $base_dir, $prepend = false)
      {
          // normalize namespace prefix
          $prefix = trim($prefix, '\\') . '\\';
  
          // normalize the base directory with a trailing separator
          $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';
  
          // initialize the namespace prefix array
          if (isset($this-&gt;prefixes[$prefix]) === false) {
              $this-&gt;prefixes[$prefix] = array();
          }
  
          // retain the base directory for the namespace prefix
          if ($prepend) {
              array_unshift($this-&gt;prefixes[$prefix], $base_dir);
          } else {
              array_push($this-&gt;prefixes[$prefix], $base_dir);
          }
      }
  
      public function loadClass($class)
      {
          // the current namespace prefix
          $prefix = $class;
  
          while (false !== $pos = strrpos($prefix, '\\')) {
  
              // retain the trailing namespace separator in the prefix
              $prefix = substr($class, 0, $pos + 1);
  
              // the rest is the relative class name
              $relative_class = substr($class, $pos + 1);
  
              // try to load a mapped file for the prefix and relative class
              $mapped_file = $this-&gt;loadMappedFile($prefix, $relative_class);
              if ($mapped_file) {
                  return $mapped_file;
              }
  
              $prefix = rtrim($prefix, '\\');
          }
  
          return false;
      }
  
      protected function loadMappedFile($prefix, $relative_class)
      {
          // are there any base directories for this namespace prefix?
          if (isset($this-&gt;prefixes[$prefix]) === false) {
              return false;
          }
  
          foreach ($this-&gt;prefixes[$prefix] as $base_dir) {
  
              $file = $base_dir
                    . str_replace('\\', '/', $relative_class)
                    . '.php';
  
              // if the mapped file exists, require it
              if ($this-&gt;requireFile($file)) {
                  // yes, we're done
                  return $file;
              }
          }
  
          // never found it
          return false;
      }
  
      protected function requireFile($file)
      {
          if (file_exists($file)) {
              require $file;
              return true;
          }
          return false;
      }
  }
  </code></pre>
  
  <p><strong>Quux.php:</strong></p>
  
  <pre><code>&lt;?php namespace Foo\Bar\Qux;
  
  class Quux {
      public function __construct() {
          echo "Hello";
      }
  }
  </code></pre>
  
  <p>Thank you for your help.</p>