title: How to use the PSR-4 autoload in my /Classes/ folder?
body: <p>I've tried multiple PSR-4 loaders, but they either don't work or I can't access the classes from another folder.</p>
  <p><strong>My current folder structure:</strong></p>
  <blockquote>
  <p>-<strong>Classes</strong></p>
  <p>--Config.php</p>
  <p>--Session.php</p>
  <p>--<strong>Frontend (folder)</strong></p>
  <p>---Login.php</p>
  </blockquote>
  <p><strong>PSR-4 Autoloader:</strong></p>
  <p>I tried to load all classes using the PSR-4 autoload register. I modified it slightly to my folder structure. I've given all classes the namespace Classes, but the ones in the Frotend folder has the namespace Classes\Frontend.</p>
  <pre class="lang-php prettyprint-override"><code>spl_autoload_register(function ($class) {
  
      // project-specific namespace prefix
      $prefix = 'Classes\\';
  
      // base directory for the namespace prefix
      $base_dir = __DIR__ . '/Classes/';
  
      // does the class use the namespace prefix?
      $len = strlen($prefix);
      if (strncmp($prefix, $class, $len) !== 0) {
          // no, move to the next registered autoloader
          return;
      }
  
      // get the relative class name
      $relative_class = substr($class, $len);
  
      // replace the namespace prefix with the base directory, replace namespace
      // separators with directory separators in the relative class name, append
      // with .php
      $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
  
      // if the file exists, require it
      if (file_exists($file)) {
          require $file;
      }
  });
  </code></pre>
  <p>I'm not sure if this has to do with the autoloader, but I also want to call the class from any file wherever it's located. So if I have a file in</p>
  <blockquote>
  <p>/Frontend/templates/login-page.php</p>
  </blockquote>
  <p>I want to be able to call the class &quot;Classes\Frontend\Login&quot;.</p>
  <p>Is this possible and how would I do that?</p>