2011-10-26 1 views
3

클래스 또는 메서드의 소스 코드를 가져 오는 방법이 있습니까? 나는 ReflectionClass을보고 있지만, 나는 보이지 않는다.사용자 정의 클래스 및 함수의 소스 코드를 가져 옵니까?

+1

파일을 열어서 php를 사용하여 파싱 할 수 있습니다. – gustavotkg

+1

여기를 확인 - http://stackoverflow.com/questions/7026690/reconstruct-get-code-of-php-function – Vikk

+0

나는 파일을 읽는 것을 피할 수 있었으면 좋겠다고 생각했지만, 오 잘. 클래스/함수가 다른 코드와 같은 줄에서 시작되지 않기를 바랄뿐입니다. – mpen

답변

6

베스트 내가 가지고 올 수 :

$class = new ReflectionClass($c); 
$fileName = $class->getFileName(); 
$startLine = $class->getStartLine()-1; // getStartLine() seems to start after the {, we want to include the signature 
$endLine = $class->getEndLine(); 
$numLines = $endLine - $startLine; 

if(!empty($fileName)) { 
    $fileContents = file_get_contents($fileName); 
    $classSource = trim(implode('',array_slice(file($fileName),$startLine,$numLines))); // not perfect; if the class starts or ends on the same line as something else, this will be incorrect 
    $hash = crc32($classSource); 
} 

편집 : 이것은 다음과 같이 정의 된 클래스와 함께 잘 작동하지 않습니다 :

class Foo { } 

이 2 선 길이 말한다 때해야 단 1 일 ...

+0

$ numLines = $ endLine - $ startLine + 1; –

+0

아주 좋아 .. 간단하고 효과적인 –