2013-10-24 76 views
1

我正試圖找到一種最好的方式來獲取文件中的所有私有,公共,靜態,受保護的方法....最好的方法是做什麼。目前,當我做的file_get_contents它轉儲整個文件,但我需要某種形式的正則表達式,這將使我的方法只獲取文件中的所有方法

$filecontent = file_get_contents($fn->getPath()."/".$fn->getFilename()); 

我不知道如果我可以用這個

preg_match("/private function | protected function | public function | public static function/") etc etc 

如果有更好的辦法,我想知道這個問題還有

+1

你不應該可以使用反射嗎? http://php.net/manual/en/book.reflection.php – Cyclonecode

+0

怎麼樣?任何例子? – Autolycus

+2

閱讀文檔總是一個好主意=) – Cyclonecode

回答

3

使用反射,假設你的路徑是PSR-0,你可以沿着線做一些事情:

<?php 

$document_root = "/document/root"; 

$file = "{$document_root}/PSR/Compatible/Path/ClassName.php"; 

$class = str_replace(
    array($document_root, DIRECTORY_SEPARATOR, ".php"), 
    array("", "\\", ""), 
    $file 
); 

$reflector = new \ReflectionClass($class); 

var_dump($reflector->getMethods()); 

?> 

希望這有助於。

+0

這不工作...給我語法錯誤[在str_replace( [$ document_root, – Autolycus

+0

如果您的PHP版本太舊,請使用'array()'語法。 – Fleshgrinder

相關問題