如何使用 phpDoc 和重载方法?
假设我有一个名为 Color 的 PHP 类,它的构造函数接受各种参数。
// hex color
$myColor = new Color('#FF008C');
// rgb channels
$myColor = new Color(253,15,82);
// array of rgb channels
$myColor = new Color(array(253,15,82));
// X11 color name
$myColor = new Color('lightGreen');
我应该如何使用 phpDoc 为构造函数和其他类似方法创建 API 文档?
如何使用 phpDoc 和重载方法?
class Color {
/**
* Constructor
* what should be here?
*/
public function __construct() {
/* CODE */
}
}
Let's say I have a PHP class called Color, it's constructor accepts various params.
// hex color
$myColor = new Color('#FF008C');
// rgb channels
$myColor = new Color(253,15,82);
// array of rgb channels
$myColor = new Color(array(253,15,82));
// X11 color name
$myColor = new Color('lightGreen');
How should I use phpDoc to create API documentation for constructor and other methods like this?
How to use phpDoc with overloaded methods?
class Color {
/**
* Constructor
* what should be here?
*/
public function __construct() {
/* CODE */
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这只是我的观点,但你首先不应该有多个构造函数 - 你的构造函数将充满 if/else 梯子,这确实不是一个好主意,特别是对于轻量级的东西,比如表示颜色。
我强烈鼓励您尝试这样的事情:
现在,在消费者代码中,而不是像这样有些神秘和模糊的构造函数调用:
相反,您可以拥有更清晰和语义的消费者代码,如下所示:
这对于阅读的人来说可能更有意义消费者代码,它消除了使不明确的构造函数工作所需的逻辑,并且作为奖励(如果您使用的是 PhpStorm 之类的 IDE),您可以通过所有检查。如果您正在运行文档生成器,这还可以确保所有选项都单独记录,而不是集中在口头描述中。
请注意,我声明了构造函数受保护 - 这是个人偏好,但如果我要拥有多个静态工厂方法,我更喜欢看到那些在消费者代码中一致使用的方法,而不是有时看到
Color::fromRGB(...)
和其他时候new Color(...)
。Just my point of view, but you should not have multiple constructors in the first place - your constructor is going to be full of if/else-ladders, which really isn't a good idea, especially for something lightweight like a representation of a Color.
I strongly encourage you to try something like this instead:
Now, in consumer code, instead of somewhat mysterious and ambiguous constructor calls like these:
Instead you can have more legible and semantic consumer code, like this:
This probably makes more sense to somebody reading the consumer code, it eliminates the logic required to make the ambiguous constructor work, and as a bonus (if you're using an IDE such as PhpStorm) you can have all your inspections pass. If you're running a documentation generator, this also ensures that all the options are documented individually, rather than lumped together in a verbal description.
Note that I declared the constructor
protected
- this is a personal preference, but if I'm going to have multiple static factory-methods, I prefer to see those consistently used in consumer code, rather than sometimes seeingColor::fromRGB(...)
and other timesnew Color(...)
.我认为最好对类/接口使用@method注释,它声明重载方法。这个问题对我来说也很有趣。
请参阅http://phpdoc.org/docs/latest/references/phpdoc/标签/method.html
I think that is better to use
@method
annotation for class/interface, which declares overloading methods. This question is interesting for me too.See http://phpdoc.org/docs/latest/references/phpdoc/tags/method.html
因为您允许可变长度参数,所以我可以通过两种方法来执行此操作。
我只想列出允许的参数是参数。
或者我会简单地用一些例子来解释。
另一种选择是,由于只有一个示例具有多个参数,因此只需在方法签名中定义参数,使最后 2 个参数成为可选。像这样:
Because you allow variable length arguments there are two ways I would do this.
I would simply list the allowed arguments are parameters.
Or I would simply provide an explanation with some examples.
Another alternative, since only one of the examples has more than one argument, would be to simply define the arguments in the method signature making the last 2 optional. Like this:
我知道没有优雅的方法可以用 phpDoc 来做到这一点。 phpDoc 注释/api 格式基于 Javadoc 格式。 Javadoc 没有功能集来支持这一点,因为在 java 中,如果您希望一个方法具有可变数量的参数,您需要为每个变体重新声明方法原型。
因此,我的性能偏好是执行类似的操作
,但这可能无法与各种自动文档工具配合使用。
根据 Hoyle 的方法来完成此操作可以在 phpDoc 中找到网站。
I know of no elegant way to do this with phpDoc. The phpDoc comment/api formatting is based on a the Javadoc format. Javadoc doesn't have a feature set to support this because in java, if you want a method to have a variable number of arguments you re-declare the method prototype for each variation.
So, my performance preference is to do something like
but this may not play nice with the various auto-documentation tools.
The according to Hoyle way to accomplish this can be found at the phpDoc site.