为常量编写 PHPDocs 的正确方法是什么?
我有这样的代码:
/**
* Days to parse
* @var int
*/
const DAYS_TO_PARSE = 10;
...
我不认为使用 @var
对于常量是正确的,并且我没有看到任何 @constant
PHPDoc 标记。这样做的正确方法是什么?
I have this code:
/**
* Days to parse
* @var int
*/
const DAYS_TO_PARSE = 10;
...
I don't think that using @var
is correct for a constant and I don't see any @constant
PHPDoc tag. What is the correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
PHP-FIG 建议使用
@var
作为常量。The PHP-FIG suggests using
@var
for constants.@const
是不是正确的答案。列出的唯一“官方”位置是 phpdoc.de,但那里的规范仅适用于1.0beta,并且该网站还包含像
@brother
和@sister
这样的标签,我以前从未见过使用过这些标签,因此对该网站的整体信任度有所下降; -) 事实上的标准一直是 phpDoc.org。
简而言之,即使某些非官方标准确实提到了它,如果文档生成器不支持它,那么它就不值得使用。
@var
目前是正确的,一旦 PSR(上面列表中的最后一个链接)不再草稿,并且是 phpDocumentor、Doxygen、APIGen 和其他人理解 PHPDoc 的基础,那么的后继者。@type
是正确的,它是@var
@const
is not the right answer.The only "official" place it's listed is phpdoc.de, but the spec there only ever made it to 1.0beta, and the site also includes tags like
@brother
and@sister
, which I've never seen used before, so the overall trust in that site is somewhat diminished ;-) The de factostandard has always been phpDoc.org.
In short, even if some unofficial standard does mention it, if the documentation generators don't support it, then it's not worth using.
@var
is correctfor now, and once the PSR (last link in the above list) is out of draft, and is the basis for which phpDocumentor, Doxygen, APIGen and others are understanding PHPDoc, then.@type
would be correct which is the successor to@var
不需要注释常量的类型,因为类型始终是:
@const
也不是 PHPDoc 标准的一部分。 PHP-FIG 建议使用@var
但这不受 PHPDoc 支持,并且不会添加任何您无法从声明本身推断出的信息。因此,为了可读性,我建议只使用一个普通的 PHPDoc 文档块来记录您的常量:
它将在您生成 PHPDocs 时描述常量,同时保持注释的干净和可读。
There is no need to annotate the type of constants, since the type is always:
@const
is also not part of the PHPDoc standard. PHP-FIG suggests@var
but this is not backed by PHPDoc and doesn't add any information you can't already deduce from the declaration itself.Therefore, for the sake of readability I recommend just using a plain PHPDoc docblock to document your constants:
It will describe the constant when you generate PHPDocs yet keeps the comments clean and readable.
我使用 Netbeans。当使用这种格式时,它将解析 phpDoc 的全局常量和类常量:
I use Netbeans. It will parse phpDoc for global and class constants when this format is used:
以下提案尊重官方文档语法:
The following proposition respects the official documentation syntax:
要将它们放入 phpDoc,请使用:
常用构造:
To get them into phpDoc, use:
Usual construct: