PHPDoc:@return void 必要吗?
是否真的有必要做这样的事情:
/**
* ...
*
* @return void
*/
我有很多没有返回值的方法,并且在注释中添加这样的内容似乎真的很多余。忽略它会被认为是不好的形式吗?
Is it really necessary do something like this:
/**
* ...
*
* @return void
*/
I have quite a few methods that don't have a return value, and it seems really redundant to put something like this in the comment. Would it be considered bad form to leave it out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果文档中的内容很清楚,则将其保留,但这并不是绝对必要的。这完全是一个主观决定。
就我个人而言,我会忽略它。
编辑
我纠正了。经过一番谷歌搜索后,维基百科页面显示:
phpdoc.org 网站说:
Sooo...基于此,我会说忽略空白。至少它是非标准的。
If it makes it clear for the documentation, then leave it in, but it isn't strictly necessary. It's an entirely subjective decision.
Personally, I would leave it out.
EDIT
I stand corrected. After a little googling, the wikipedia page says:
The phpdoc.org website says:
Sooo... Based on that, I would say leave out the void. It's non-standard, at least.
根据 phpDocumentor,@return void 是有效的:
http://www.phpdoc .org/docs/latest/guides/types.html#keywords
资料来源: http://www.phpdoc.org/docs/latest /for-users/phpdoc/types.html (存档页面)。
According to phpDocumentor, @return void is valid:
http://www.phpdoc.org/docs/latest/guides/types.html#keywords
Source: http://www.phpdoc.org/docs/latest/for-users/phpdoc/types.html (archived page).
由于我最近学到的一些东西,我必须编辑我的答案。
使用
@return void
代替@return null
具有非常特殊的含义,请考虑以下两个 PHP 代码示例。在第一个示例中,PHP 实际上将返回
NULL
,因为 PHP 始终返回NULL
。但返回的值对调用者来说没有用,因为它没有说明该函数做了什么。 IDE 可以使用@return void
的记录信息来指示开发人员使用了无用的返回值。第一个调用是毫无意义的,因为变量将始终包含
NULL
,第二个调用实际上可能包含某些内容。如果我们将函数调用放入条件中,这会变得更加有趣。正如您所看到的,
@return void
有它的用例,如果适用的话应该使用。另请注意,它将成为即将推出的 PHP PSR-5 标准的一部分。[1]
[1] http://www.php-fig.org/psr/
I have to edit my answer because of something I have learned recently.
Using
@return void
instead of@return null
has a very special meaning, consider the following two examples of PHP code.In the first example PHP will actually return
NULL
, since PHP always returnsNULL
. But the returned value is of no use to the caller since it does not say anything about what the function did. IDEs can use the documented information of@return void
to indicate the developer that a return values is used which serves no purpose.The first call is senseless since the variable will always contain
NULL
, the second one might actually contain something. This is becoming even more interesting if we put the function calls into a conditional.As you can see,
@return void
has its use cases and should be used if applicable.Also note that it is going to be a part of the upcoming PHP PSR-5 standard.[1]
[1] http://www.php-fig.org/psr/
从 php 7.1 开始,
void
是有效的返回类型,并且可以在函数上强制执行。我总是将其添加到文档块中。
编写它的另一个好处是,将
void
方法与可能返回任何内容但由于疏忽而在文档块上没有@return
条目的方法区分开来。As of php 7.1,
void
is a valid return type and can be enforced on a function.I would always add it on the docblock.
Another benefit of writing it, is to differentiate the
void
methods from the methods that may return anything but don't have a@return
entry on the docblock by negligence.以下是我对 PhpDocumentor 注释的理解和使用:
Here is how I understand and use PhpDocumentor annotations:
就我个人而言,我认为这里缺少的一件大事是记录函数返回结果非常重要。目前标准没有任何关于从不返回的函数的文档......因此 return void 是表示该函数确实返回的方式。
考虑这个代码块
显然,使用 @return 至少表明该函数确实返回
Personally, I think the big thing missing from this is that documenting a function returns at all is important. Currently standards dont have any documentation for functions that never return....hence a return void is way of saying yes this function does actually return.
Consider this code block
Clearly the use of @return at least indicates the function does return