设置和记录无限参数运行的正确方法?
背景: 我正在创建一个方法 addAll
,它将传递给该方法的所有值添加到我的类中。这就是我的想法:
public function addAll() {
if(func_num_args()===0) {
throw new BadMethodCallException(get_class($this).'::addAll() must have arguments.');
}
$args = func_get_args();
foreach($args as &$arg) {
$this->add($arg);
}
}
而且效果很好。然后我用 phpDocumentor: 来记录它
/**
* @param mixed ... All of the values to add.
*/
。 。 。但我没有我的 @param
的名称,因为坦率地说,它不存在。
问题:我如何构造和定义这样的东西?
因为我希望它至少传入一个值,所以我想出了这个,但我不确定:
/**
* @param mixed $value,... All of the values to add.
*/
public function addAll($value) {
$args = func_get_args();
foreach($args as &$arg) {
$this->add($arg);
}
}
这似乎很错误,因为 $ value
从未被直接使用。 。 。
另外,我已经有 add
来添加一个,所以 addAll
在语义上不应该至少需要两个参数吗?您建议如何定义和记录它?
Background:
I am creating a method addAll
that adds all of the values passed to the method to my class. Here's what I was thinking:
public function addAll() {
if(func_num_args()===0) {
throw new BadMethodCallException(get_class($this).'::addAll() must have arguments.');
}
$args = func_get_args();
foreach($args as &$arg) {
$this->add($arg);
}
}
And it works great. Then I got to documenting it with phpDocumentor:
/**
* @param mixed ... All of the values to add.
*/
. . . but I don't have a name for my @param
because, frankly, it doesn't exist.
Question: How do I construct and define something like this?
Since I want it to have at least one value passed in, I came up with this but am unsure:
/**
* @param mixed $value,... All of the values to add.
*/
public function addAll($value) {
$args = func_get_args();
foreach($args as &$arg) {
$this->add($arg);
}
}
It seems so wrong because $value
is never directly used. . .
Also, I already have add
which adds one, so shouldn't addAll
semantically require at least two parameters? What would you recommend for defining and documenting that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案:使用 @example 标签。
我还更改了我的设置,这似乎是利用 PHP 所提供的功能而不丧失可用性的最佳方式:
Answer: use an @example tag.
I also changed my setup, and it seems to be the best way to utilize what PHP has to offer without forfeiting usability: