SO 上已经有 2 个类似的此类问题,但似乎没有一个答案有效。
PHPDoc 似乎没有将我的函数中的可选参数识别为可选参数,例如:
/**
* Opens the connection and sets encoding
*
* @param string $encoding Encoding.
*/
public function __construct($encoding='UTF-8')
{
$this->connect_mysqli();
$this->set_encoding_mysqli($encoding);
}
它不应该将 $encoding 识别为可选的,还是我在这里遗漏了什么?我真的尝试用谷歌搜索并阅读文档,但我发现的只是:
If you are not indicating in the actual code that the parameter is optional (via "$paramname = 'a default value'"), then you should mention in the parameter's description that the parameter is optional.
所以我发现我的代码没有问题,但我在文档中得到的只是: “__construct(string $encoding)”,参数可选的任何地方都没有标记。
最佳答案
严格来说,PHP 不知道“可选参数”,而是有默认值的参数,在调用函数或方法时可以省略。好的,最后是一个可选参数,但是你的
@param string $encoding Encoding.
在这里是完全正确的,因为默认值是一个字符串。文档试图告诉您的是,您应该像自己一样提及它
@param string $encoding (optional) Encoding.
我同意你的观点,像这样的符号
__construct([$encoding])
或
__construct($encoding = 'UTF-8')
会很好。您可以发布错误报告
https://github.com/phpDocumentor/phpDocumentor2/issues?state=open
更新:意识到这已经提到了https://github.com/phpDocumentor/phpDocumentor2/search?q=optional&type=Issues
关于PHPDoc 可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13441333/