을 가지고 PHP 버전이 필요합니다. 주어진 파일을 검사하고 그 중에서 파일에 매핑 된 클래스 목록을 작성하는 강력한 스크립트를 만드는 것은 어렵지 않습니다. 여기에 내가 그것을 할 방법은 다음과 같습니다
파일 매핑을 생성하는 스크립트의
$classes = array();
//this is the main function, give it a file and it will map any
//classes it finds in the file to the path. How you find the files
//is up to you, only you know your directory structure, but I
//generally set up a few folders that hold my classes, and have
//the script recurse through those passing each file it finds through
//this function
function get_php_classes($file) {
global $classes;
$php_code = file_get_contents($file);
$tokens = token_get_all($php_code);
$count = count($tokens);
//uses phps own parsing to figure out classes
//this has the advantage of being able to find
//multiple classes contained in one file
for ($i = 2; $i < $count; $i++) {
if ( $tokens[$i - 2][0] == T_CLASS
&& $tokens[$i - 1][0] == T_WHITESPACE
&& $tokens[$i][0] == T_STRING) {
$class_name = $tokens[$i][1];
//now we map a class to a file ie 'Autoloader' => 'C:\project\Autoloader.cls.php'
$classes[$class_name] = $file;
}
}
}
$fh = fopen('file_you_want_write_map_to', 'w');
fwrite($fh, serialize($classes));
fclose($fh);
, 당신은 새로운 클래스를 추가하면 언제든지 실행합니다.
class Autoloader {
private $class_map;
public function __construct() {
//you could also move this out of the class and pass it in as a param
$this->class_map = unserialize(file_get_contents($file_you_wrote_to_earlier));
spl_autoload_register(array($this, 'load'));
}
private function load($className) {
//and now that we did all that work in the script, we
//we just look up the name in the map and get the file
//it is found in
include $this->class_map[$className];
}
}
더 그는이와 같은 확인하고, 자동로드 목록을 구축하는 동안 발견 중복 클래스 등 다양한 것들에 대한 확인, 즉 안전을 할 수있는 많은있다 : 여기에 자동로드에 사용할 수있는 실제 응용 프로그램 코드는 것을 파일은
왜 클래스 자동 로딩과 네임 스페이스를 사용하지 당신을 위해 모든합니까? –
Autoloader ....가 필요하고 include/require가 항상 절대 경로를 사용할 수있는 것처럼 들립니다. –
작곡가 : http://getcomposer.org – igorw