title: Dynamically Loading Namespaced Classes
body: <p>I'm working on a Data Mapper framework for PHP that will use Namespaces and rely exclusively on PHP5.3. In order to allow others to use the framework and extend its inner components as needed, I'd like to support a form of dynamic autoloading that will load the requested class file first from any custom namespaces and lastly from the framework namespace. </p>
  
  <p>So far I have:</p>
  
  <pre><code>class Gacela {
  
  protected static $_instance;
  
  protected $_namespaces = array();
  
  protected $_sources = array();
  
  protected $_mappers = array();
  
  protected $_resources = array();
  
  protected function __construct()
  {
      spl_autoload_register(array(__CLASS__, 'autoload'));
  
      $this-&gt;registerNamespace('Gacela', dirname(realpath(__FILE__)));
  }
  
  protected function _findFile($file)
  {
      if(file_exists($file) &amp;&amp; is_readable($file)) {
          return true;
      }
  
      return false;
  }
  
  public static function autoload($class)
  {
      $parts = explode("\\", $class);
      $self = self::instance();
      $return = false;
  
      if(isset($self-&gt;_namespaces[$parts[0]])) {
          $file = $self-&gt;_namespaces[$parts[0]].str_replace("\\", "/", $class).'.php';
  
          if($self-&gt;_findFile($file)) {
              $return = $class;
          }
      } else {
  
          $namespaces = array_reverse($self-&gt;_namespaces);
  
          foreach ($namespaces as $ns =&gt; $path) {
              $file = $path.$ns.str_replace("\\", "/", $class).'.php';
  
              if($self-&gt;_findFile($file)) {
                  $return = $ns . $class;
                  break;
              }
          }
      }
  
      require $file;
  
      return $return;
  }
  
  public static function instance()
  {
      if(is_null(self::$_instance)) {
          self::$_instance = new Gacela();
      }
  
      return self::$_instance;
  }
  }
  </code></pre>
  
  <p>Unfortunately, it just goes to a white screen whenever I try to load any classes with it. </p>
  
  <p>If you would like to see the full framework code, it can be downloaded from <a href="https://github.com/gabriel1836/gacela" rel="nofollow">github</a>.</p>
  
  <p>Can anyone provide insight on this could accomplished?</p>