PHPDoc 用于可变长度参数数组
是否有用于记录采用单个配置数组而不是单个参数的函数的语法?
我特别想到 CodeIgniter 风格的库,它使用与此类似的机制:
<?php
//
// Library definition
//
class MyLibrary {
var $foo;
var $bar;
var $baz;
// ... and many more vars...
/* Following is how CodeIgniter documents their built-in libraries,
* which is mostly useless. AFAIK they should be specifying a name
* and description for their @param (which they don't) and omitting
* @return for constructors
*/
/**
* @access public
* @param array
* @return void
*/
function MyLibrary($config = array()) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
}
//
// Library usage:
//
// Iniitialize our configuration parameters
$config['foo'] = 'test';
$config['bar'] = 4;
$config['baz'] = array('x', 'y', 'z');
$x = new MyLibrary($config);
?>
所以我的问题是,除了纯粹的文本描述之外,是否有某种受支持的方法来记录配置数组?实际上指定一个正确的@param [type] [name] [desc]
来允许PHPDoc解析出有用的值?
顺便说一句,CodeIgniter 实际上只是用上面通过 $config 数组传入的值覆盖它自己的值,从而有效地允许您破坏私有成员。我不是粉丝,但我坚持下去。
Is there a syntax for documenting functions which take a single configuration array, rather than individual parameters?
I'm thinking specifically of CodeIgniter-style libraries, which use a mechanism similar to this:
<?php
//
// Library definition
//
class MyLibrary {
var $foo;
var $bar;
var $baz;
// ... and many more vars...
/* Following is how CodeIgniter documents their built-in libraries,
* which is mostly useless. AFAIK they should be specifying a name
* and description for their @param (which they don't) and omitting
* @return for constructors
*/
/**
* @access public
* @param array
* @return void
*/
function MyLibrary($config = array()) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
}
//
// Library usage:
//
// Iniitialize our configuration parameters
$config['foo'] = 'test';
$config['bar'] = 4;
$config['baz'] = array('x', 'y', 'z');
$x = new MyLibrary($config);
?>
So my question is, is there some supprted way of documenting the configuration array beyond just the purely textual description? Actually specifying a proper @param [type] [name] [desc]
that allows PHPDoc to parse out useful values?
As an aside, CodeIgniter really does just overwrite it's own values with those passed in via the $config array as above, effectively allowing you to clobber private members. I'm not a fan, but I'm stuck with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我从来没有见过任何“好的”方式来记录这一点——而且我也从未见过任何可以被 IDE 使用的东西(例如 Eclipse PDT) 来暗示参数:-(
我会已经说过“像你的框架那样做”,但正如你所说,它在这里所做的还不够好......
不过,也许可能的键的快速/排序列表可能比没有好;有点像这样:
不确定 phpDocumentor 或 IDE 将如何解释它...但可能值得一试?
顺便说一句,这就是我倾向于避免这种传递参数的方式的原因之一 - 至少当方法没有太多(可选)参数时。
I've never seen any "good" way of documenting this -- and I've never seen anything that could be used by IDEs (such as Eclipse PDT) for parameters hinting either :-(
I would have said "do like your framework does", but as you said, what it does, here, is not quite good enough...
Maybe a quick/sort list of possible keys might be better than nothing, though ; a bit like this :
Not sure how it would be interpreted by phpDocumentor or an IDE... But might be worth a try ?
This is, btw, one reason why I tend to avoid that kind of way of passing parameters -- at least when there are not too many (optional) parameters to a method.
数组的正确数组 @param 表示法在 PHPlint 中指定,
您可以使用它以有用的方式记录配置数组:
示例:
The correct array @param notation for arrays is as specified in PHPlint
You can use it to document a config array in a useful manner:
Example:
你可以这样做:
netbeans 会拾取它,但 phpdoc 会弄乱文档
You can do this:
and netbeans will pick it up but phpdoc messes up the documentation
在这种情况下我总是使用
我使用过的大多数 IDE 和文档生成器似乎都以合理的方式呈现此内容,尽管它们当然不提供任何类型检查或数组参数检查。
I always use
<pre>
tags in situations like this. Ex.:Most IDEs and documentation generators I have used seem to render this in a reasonable way, though of course they don't provide any type checking or inspection of the array parameters.
目前没有“官方”(如“由多种工具支持”)的方式来做到这一点。
PHP Fig 目前正在 https://groups.google 上讨论该问题.com/d/topic/php-fig/o4ko1XsGtAw/discussion
There is currently no "official" (as in 'supported by multiple tools') way to do this.
The PHP FIG is discussing it at the moment at https://groups.google.com/d/topic/php-fig/o4ko1XsGtAw/discussion
文本描述,无论您想要何种程度的完整性,实际上都是您唯一的选择。您可以根据需要使其清晰易读,但代码分析工具(phpDocumentor、IDE 支持)无法了解您的
$array
在运行时的实际结构。我同意许多评论者的观点,即以这种方式编写代码可以用编码的便利性换取代码的易读性。
A text description, to whatever degree of completeness you want, is really your only option. You can make it as legible as you want, but code analysis tools (phpDocumentor, IDE support) have no way to know how your
$array
is actually structured at runtime.I agree with the many commenters that writing code this way exchanges coding convenience for code legibility.
我用过类。
它工作得相当好,主要问题是代码中充斥着特定的类。它们可以嵌套,以便可以重用部分配置。
I've used classes.
It works fairly well, main problem is that the code becomes littered with specific classes. They can be nested so parts of configuration can be reused.