2011-06-13 61 views
1

我想自動加載我的PHP 5.3命名空間類例如。如何自動加載PHP 5.3類?

/JM 
    Auth.php 
    User.php 
    Db/Entity.php 
    /... 

我做

namespace KM; 

class Autoloader { 
    public static function registerAutolaod() { 
     ini_set('include_path', dirname(__FILE__) . PATH_SEPARATOR . ini_get('include_path')); 
     spl_autoload_register(function ($classname) { 
      $file = preg_replace('/\\\/', DIRECTORY_SEPARATOR, $classname) . '.php'; 
      echo $file . '<br />'; 
      include ($file); 

     }); 
    } 
} 

問題是有時我得到的類名作爲User有時KM\User怎樣才能解決這個問題?

回答