2010-03-11 35 views
1

我想要提取方法的意見,採取以下方法,例如:獲取PHP的方法評價

/** 
* Returns the regex to extract all inputs from a file. 
* @param string The class name to search for. 
* @return string The regex. 
*/ 
public function method($param) 
{ 
    //... 
} 

結果應該是

Returns the regex to extract all inputs from a file. 
@param string The class name to search for. 
@return string The regex. 

我找到的方法是使用一個函數像file_get_content到獲取文件內容 - >過濾我想要的方法 - >取得評論使用正則表達式

它似乎有點複雜,有沒有任何方便的方法來歸檔呢?

回答

4

如果你想在PHP中使用的東西的評論檢查出PHP的reflection api

+0

感謝,正是我想:) – limboy 2010-03-11 08:27:57

+0

深入瞭解後,這不是我所需要的。 getDocComment只是獲取類的評論,而不是方法。所以我寫了一個小腳本來做這個http://gist.github.com/329113 – limboy 2010-03-11 13:14:21

1

PHP Doc。像Java Doc一樣。

1

其實你可以得到一個方法的文檔註釋getDocComment與getDocComment

$ref=new ReflectionMethod('className', 'methodName'); 

echo $ref->getDocComment(); 
0

對於我用這個小方法轉儲功能我組成。 它從公開提供的類中提取所有方法(因此對您有用)。

我個人使用dump()方法,以良好的格式化方法的名稱和說明的輸出陣列,但是這是沒有必要的,如果你想用它來做別的東西:-)

function getDocumentation($inspectclass) { 
    /** Get a list of all methods */ 
    $methods = get_class_methods($inspectclass); 
    /** Get the class name */ 
    $class =get_class($inspectclass); 
    $arr = []; 
    foreach($methods as $method) { 
     $ref=new ReflectionMethod($class, $method); 
     /** No use getting private methods */ 
     if($ref->isPublic()) { 
      $arr[$method] = $ref->getDocComment(); 
     } 
    } 
    /** dump is a formatting function I use, feel free to use your own */ 
    return dump($arr); 
} 
echo getDocumentation($this);