php PHPDoc 可选参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13441333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:27:10  来源:igfitidea点击:

PHPDoc optional parameter

phpphpdocparamoptional

提问by KingCrunch

There are already 2 similar questions of this type here on SO but none of the answers seem to work.

在 SO 上已经有 2 个类似的此类问题,但似乎没有一个答案有效。

PHPDoc doesn't seem to recognize optional parameters in my functions as optional, for example:

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);
}

Shouldn't it recognize $encoding as being optional or am I missing something here? I really tried to google and read the documentation but all I found is:

它不应该将 $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.

如果您没有在实际代码中指明该参数是可选的(通过“$paramname = 'a default value'”),那么您应该在参数的描述中提及该参数是可选的。

So I see no problem with my code, but all I get in documentation is: "__construct(string $encoding)", no sign anywhere that parameter is optional.

所以我认为我的代码没有问题,但我在文档中得到的只是:“__construct(string $encoding)”,该参数是可选的任何地方都没有符号。

回答by KingCrunch

Strictly speaking, PHP doesn't know "optional parameters", but parameters with default values, that can be omitted when the function or method is called. OK, that's at the end an optional parameter, but your

严格来说,PHP 不知道“可选参数”,而是具有默认值的参数,在调用函数或方法时可以省略这些参数。好的,最后是一个可选参数,但是你的

@param string $encoding Encoding.

is completely correct here, because the default value is a string. What the documentation tries to tell you is that you should mention it yourself like

这里是完全正确的,因为默认值是一个字符串。文档试图告诉您的是,您应该自己提及它

@param string $encoding (optional) Encoding.

I agree with you, that a notation like

我同意你的看法,像这样的符号

__construct([$encoding])

or

或者

__construct($encoding = 'UTF-8')

would be nice. You can post a bug report

会好的。您可以发布错误报告

https://github.com/phpDocumentor/phpDocumentor2/issues?state=open

https://github.com/phpDocumentor/phpDocumentor2/issues?state=open

Update: Realised, that this already mentioned https://github.com/phpDocumentor/phpDocumentor2/search?q=optional&type=Issues

更新:意识到,这已经提到了https://github.com/phpDocumentor/phpDocumentor2/search?q=optional&type=Issues