2012-10-11 2 views
0

PHP에 클래스 경로와 의존성 관리 시스템이 없다는 것이 문제가되는 한가지입니다. 제안 할 수있는 프레임 워크가 있습니까? 배가 들여다 볼 수있는 좋은 시스템이라고 들었지만, 그 밖에 무엇이 있는지 알고 싶습니다.PHP 의존성과 클래스 패스 관리

예를 들어 ... A.php, B.php 및 C.php 파일이 있습니다. A는 B에 의존하며 C는 모두 3이 다른 폴더에 있습니다.

A.php에 B.php가 포함 된 후에는 C.php도 포함시켜야합니다. require_once가 B.php와 C.php 사이가 아닌 A.php와 C.php 사이의 상대 경로를 호출해야하기 때문에 B.php에서 require_once ("C.php")를 입력하면 작동하지 않습니다. 이는 성가신 일입니다.

+0

왜 클래스 자동 로딩과 네임 스페이스를 사용하지 당신을 위해 모든합니까? –

+0

Autoloader ....가 필요하고 include/require가 항상 절대 경로를 사용할 수있는 것처럼 들립니다. –

+0

작곡가 : http://getcomposer.org – igorw

답변

2

doctrine 클래스 로더 프로젝트를 사용해 보시기 바랍니다.

Here 공식 문서를 찾을 수 있습니다.

는이 라이브러리를 사용하려면 네임 스페이스 지원 내가 오토로더를 선호하는 경향이 문제에 대한 (다음> = 5.3)

3

을 가지고 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]; 
    } 
} 

더 그는이와 같은 확인하고, 자동로드 목록을 구축하는 동안 발견 중복 클래스 등 다양한 것들에 대한 확인, 즉 안전을 할 수있는 많은있다 : 여기에 자동로드에 사용할 수있는 실제 응용 프로그램 코드는 것을 파일은

1

작곡가는 모두가 일어나고 어디 등을 포함하기 전에 존재하고, 그것은 아주 편하게

http://getcomposer.org/