2009-08-30 80 views
3

如何描述PHP函数的参数和返回类型(getter/setter)?有效描述PHP函数

我需要告诉我的主持人返回类型并列出每个函数的参数。 我有数百个函数,所以这成为问题,因为我需要为每个修订版都这样做。

我目前使用以下步骤

  1. ack-grep "function " > /tmp/functions 2。在Vim中:
    • %s/\d//g, %s/{//, %s/://g
    • '%s的/.* PHP/\ü&/
    • 然后把大写文件名的每个文件的功能列表中的开头
    • 放功能。一个文件和参数到另一文件,使得linenumbers匹配
    • 创建第三文件你写要么settergetter为相应线路
  2. paste -d"&" /tmp/{functions,functions_param,functions_type}
  3. 加乳胶格式每个组的每个文件
+0

谢谢您的回答! – 2009-08-30 07:35:24

回答

8

使用类似phpdoc的功能。

基本上你添加特殊注释代码:

/** 
* A sample function docblock 
* @global string document the fact that this function uses $_myvar 
* @staticvar integer $staticvar this is actually what is returned 
* @param string $param1 name to declare 
* @param string $param2 value of the name 
* @return integer 
*/ 
function firstFunc($param1, $param2 = 'optional') { 
    static $staticvar = 7; 
    global $_myvar; 

    return $staticvar; 
} 

,它会自动为它生成的HTML文档。

基本上,这背后的想法是让程序员的生活更轻松,并且允许编写内联API文档,而不必花费大量时间。

有一些IDE也能理解这一点,并且会在您使用它时显示文档。例如,函数:

/** Retrieve the action key 
* @return string 
*/ 
function isValid($value) { 
    .... 
} 

这显示在Zend Studio的:http://static.zend.com/topics/code-assist.png

特别是如果你使用像这样的IDE(还有其他人,除了那个的Zend做),你可能会发现你自然记录每个功能和参数,因为它可以帮助你,而你编码反正。

+0

我在一个文件中有很多功能。 **如果我将注释块放在每个函数的开头,这样我不需要在单独的文件中具有这些函数,这是否工作?** – 2009-08-30 07:25:07

+0

是的。您还可以评论变量,类和定义。 – gregmac 2009-08-30 07:33:00

4

phpdoc。以加两个数字的和函数并返回结果为例:

/** 
* Adds up two int numbers 
* @param int $x the first number to add 
* @param int $y the second number to add 
* @return int the result of the operation 
*/ 
function my_sum ($x, $y) 
{ 
    return $x+$y; 
}