25

我在我的 PHP 应用程序中使用了几个关联数组,并且我正在使用 PHP 文档器来评论我的源代码。我从来没有真正为数组中的数组指定注释,但现在我需要这样做并且不知道如何。

$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部分执行此操作?

4

3 回答 3

33

你不能记录每个键,但你可以告诉 phpDocumentor 它是什么类型

你可以这样做:

/**
 * Form the array like this:
 * <code>
 * $array = array(
 *   'id'      => 'foo',          // the id
 *   'class'   => 'myClass',     // the class
 * );
 * 
 * </code>
 *
 * @var array[string]string 
 */
$array;
于 2010-04-27T20:57:44.043 回答
9

我会查看WordPress 内联文档参考以获取一些提示,尽管它目前并不全面。

使用@param 或@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'.
 * }
 */
于 2013-10-29T20:19:41.587 回答
5

对我来说,这在 PhpStorm 中可以很好地描述返回值:

/**
 * @param string $requestUri
 * @return array[
 *  'controller' => string,
 *  'action' => string
 * ]
 */
于 2019-07-08T12:40:19.073 回答