title: Efficient PHP auto-loading and naming strategies
body: <p>Like most web developers these days, I'm thoroughly enjoying the benefits of solid MVC architecture for web apps and sites.  When doing MVC with PHP, autoloading obviously comes in extremely handy.</p>
  
  <p>I've become a fan of <a href="http://www.php.net/spl_autoload_register" rel="noreferrer"><code>spl_autoload_register</code></a> over simply defining a single <code>__autoload()</code> function, as this is obviously more flexible if you are incorporating different base modules that each use their own autoloading.  However, I've never felt great about the loading functions that I write.  They involve a lot of string checking and directory scanning in order to look for possible classes to load.</p>
  
  <p>For example, let's say I have an app that has a base path defined as <code>PATH_APP</code>, and a simple structure with directories named <code>models</code>, <code>views</code> and <code>controllers</code>.  I often employ a naming structure whereby files are named <code>IndexView.php</code> and <code>IndexController.php</code> inside the appropriate directory, and models generally have no particular scheme by default. I might have a loader function for this structure like this that gets registered with <code>spl_autoload_register</code>:</p>
  
  <pre><code>public function MVCLoader($class)
  {
      if (file_exists(PATH_APP.'/models/'.$class.'.php')) {
          require_once(PATH_APP.'/models/'.$class.'.php');
          return true;
      }
      else if (strpos($class,'View') !== false) {
          if (file_exists(PATH_APP.'/views/'.$class.'.php')) {
              require_once(PATH_APP.'/views/'.$class.'.php');
              return true;
          }
      }
      else if (strpos($class,'Controller') !== false) {
          if (file_exists(PATH_APP.'/controllers/'.$class.'.php')) {
              require_once(PATH_APP.'/controllers/'.$class.'.php');
              return true;
          }
      }
      return false;
  }
  </code></pre>
  
  <p>If it's not found after that, I might have another function to scan sub-directories in the models directory. However, all the if/else-ing, string checking and directory scanning seems inefficient to me, and I'd like to improve it.</p>
  
  <p>I'm very curious what file naming and autoloading strategies other developers might employ. I'm looking specifically for good techniques to employ for efficient autoloading, and not alternatives to autoloading.</p>