2014-09-27 90 views
0

比方說,我有:通過讀取功能評論(PHP)自動創建文件

class myclass { 

    /* 
    * This function, bla bla bla 
    */ 
    function myclass() 
    { 
     return(true); 
    } 

    /* 
    * This function, bla bla bla 
    */ 
    function myfunc1() 
    { 
     return(true); 
    } 

    /* 
    * This function, bla bla bla 
    */ 
    function myfunc2() 
    { 
     return(true); 
    } 
} 

通過使用get_class_methods(new myclass());我能得到的類。

現在我的問題是這樣的:我可以從類函數讀取註釋到字符串中嗎? 所以我可以創建自動生成的文檔。

+1

使用[phpDocumentor的(http://www.phpdoc.org/),和其他人一樣做;但docblocks應該來___before___方法定義,而不是在他們內部 – 2014-09-27 11:09:21

+0

謝謝@MarkBaker我知道PHPDocumentor,但我想自己做一些事情:-) 更新了示例,我仍然想知道我怎麼能得到這些評論: - P – 2014-09-27 11:24:16

+0

如果您想重新發明輪子,請使用[反射](http://www.php.net/manual/en/book.reflection.php),特別是'getDocComment()'方法....但注意,'爲了用這種方法檢索評論,評論必須在類,函數或方法之前立即執行' – 2014-09-27 11:31:54

回答

3

您無法訪問來自PHP的評論,目標文件包含在include,include_once,require或require_once中,因爲php-parser剝離了代碼中的所有註釋。

如果您需要這樣做並且不想使用PHPDocumentor或Doxygen,但又想自己動手,那麼您需要使用file_get_contents或任何其他讀取方法來關注目標文件,並使用reguar表達式自己分析此代碼你選擇的其他方法(例如使用這個庫 - https://github.com/nikic/PHP-Parser)。並且自己解析代碼,您可以從評論中獲得所需的全部信息。

但它也不是那麼容易的事,所以我的建議是使用的phpDocumentor:P

+1

+1推薦Nikic的解析器 – 2014-09-27 11:37:46

+0

是的。我喜歡PHPDoc的文檔標準:) +1 – iFarbod 2014-09-27 11:40:17

+0

感謝@dmikam!是的,我知道我很固執,但真的想嘗試一下; --P – 2014-09-27 14:49:13