2011-06-13 4 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); 

     }); 
    } 
} 

문제가 가끔 어떻게이 문제를 해결할 수 KM\UserUser로 클래스 명을 얻을 때로는나요?

답변

1

콜백 함수로 수신되는 $classname은 로컬로 간주됩니다. 네임 스페이스 내에 __autoloader를 정의했기 때문입니다.

절대/정규화 된 네임 스페이스와 클래스 이름을 항상 얻으려면 자동 로더를 전역 범위로 선언하십시오. http://www.php.net/manual/en/language.oop5.autoload.php#87985

관련 문제