2009-09-06 78 views
13

实施例:phpdoc标准用于设置可选参数的默认值?

/** 
* This function will determine whether or not one string starts with another string. 
* @param string $haystack <p>The string that needs to be checked.</p> 
* @param string $needle <p>The string that is being checked for.</p> 
* @param boolean $case[optional] <p>Set to false to ignore case(capital or normal characters)</p> 
* @return boolean <p>If the $haystack string does start with the $needle string, the return will be true. False if not.</p> 
*/ 
function endsWith($haystack,$needle,$case=true) { 
    if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);} 
    return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0); 
} 

可选参数是默认设置为true。我希望说明文档中的默认设置。有没有这样做的标准方式,还是我不得不在描述中提及它?

回答

13

The doc says

注意$ PARAMNAME,...将在两个 参数列表和功能 签名输出文档显示 。如果在参数 是可选参数(通过“$ paramname ='a 默认值”“))的实际代码中没有指示 ,那么您应该在参数的说明 中提及该参数是可选的。

所以,如果你不显示在函数签名的默认分配,这将是一个好主意,包括它的描述,但在你的情况包括它的签名。所以,你不需要改变一件事情,除非这样做会让你感觉更好。

+0

谢谢。它确实让我感觉更好,是的:) – KdgDev 2009-09-06 22:48:56

+0

这是在函数签名中包含可选参数的情况下的答案。但如果不是呢?如何记录可选参数?从文档来看,唯一的方法是在描述中说明它。所以没有办法写例如'[$ case = true]'。即使是jsdoc也有它。 – FreeLightman 2018-03-07 13:20:47