2010-04-26 23 views
17

我在我的PHP应用程序中使用几个关联数组,我使用PHP documentor来评论我的来源。我从来没有真的为数组中的数组指定注释,但现在我需要这样做,不知道如何。评论关联数组在PHP Documentor

$array = array('id' => 'test', 'class' => 'tester', 'options' => array('option1' => 1, 'option2' => 2)) 

如何评论这个数组中的@var@param意见正确的方法是什么? 我能做到这一点是这样,但我不知道这是否是正确的:

@param string $array['id'] 
@param string $array['class'] 
@param int $array['options']['option1'] 

但如何为@var部分做到这一点?

回答

29

您不能记录每个密钥,但是you can tell phpDocumentor what type it is

你可以做这样的事情:

/** 
* Form the array like this: 
* <code> 
* $array = array(
* 'id'  => 'foo',   // the id 
* 'class' => 'myClass',  // the class 
*); 
* 
* </code> 
* 
* @var array[string]string 
*/ 
$array; 
+0

感谢您的帮助;) – Abenil 2010-05-02 01:06:12

+2

这已被证实可以在任何IDE中使用auto-complete/intellisense,我想知道吗?根据[类型定义的phpDoc ABNF](http://www.phpdoc.org/docs/latest/for-users/types.html#abnf),没有为数组索引指定类型的限制。它将数组指定为'@var string []'('array'组件仅适用于“未指定”数组)。 – Sepster 2012-09-08 16:40:49

+0

@Sepster不幸的是,我不认为大多数IDE都足够聪明来认识到这一点。你的里程可能会有所不同,但我甚至发现Zend Studio的实现有点缺乏,当涉及到这种类型的意识类型。 – 2012-09-18 22:28:48

8

我会看着WordPress Inline Documentation Reference了一些提示,虽然现在没有全面的。

使用@参数或@var或@property,两者都适合在上下文

根据这些准则,你可能会记录您的关联数组是这样的:

/** 
* @property array $my_array { 
*  An array of parameters that customize the way the parser works. 
* 
*  @type boolean $ignore_whitespace Whether to gobble up whitespace. Default true. 
*  @type string $error_level What the error reporting level is. Default 'none'. 
*        Accepts 'none', 'low', 'high'. 
* } 
*/ 
+1

尽管认真讨论过,但记录数组结构的这种记法从未将它用于官方PHPDoc规范2013-14关于添加它。 – 2016-12-14 20:50:17

+1

似乎是在https://github.com/phpDocumentor/phpDocumentor2/issues/650一些相关的讨论 – 2017-01-24 01:45:43