2015-08-28 52 views
1

如何指定數組索引和子索引類型? 注意:我會用它與PHPStorm。指定數組索引類型

陣列例如

function name ($options) { 
    // $options['length']  => integer 
    // $options['more']   => array 
    // $options['more']['test1'] => boolean 
    // $options['more']['test2'] => boolean 
} 

不是工作):

/** 
* @param array $options 
*  @var int $length 
*  @var array $more 
*   @var bool $test1 
*   @var bool $test2 
*/ 
+0

[最好的方式來文檔數組選項在PHPDoc?](http://stackoverflow.com/questions/15414103/best-way-to-document-array-options-in-phpdoc) – rjdown

回答

2

一般來說,PhpStorm只支持簡單的語法,就像Sam所說的,例如,

/** 
* @param string[] $options 
*/ 

上面描述的參數是字符串數組。


安裝選項完成插件 - 它支持哈希(描述數組鍵和類型)在PHPDoc的新提出的語法:https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#7-describing-hashes

這個插件將添加代碼完成對數組的鍵。

<?php 
class Element { 
    /** 
    * Initializes this class with the given options. 
    * 
    * @param array $options { 
    *  @var bool $required Whether this element is required 
    *  @var string $label The display name for this element 
    * } 
    */ 
    public function __construct(array $options = array()) 
    { 
     // some code here 
    } 
} 


new Element(['label' => 'Bob', '|' ]); 
//        | Ctrl+Space will show supported attributes 

注:此插件的主要目的是提供數組鍵完成 - 我不知道它如何支持每個數組元素的類型分辨率(在情況下,如果他們喜歡在不同的你例)。

0

它看起來像,according to the docs,它的唯一可能定義一個陣列作爲一組特定類型(而不是爲每個索引設置類型):

/** 
* @param string[] $options 
*/ 

更好的解決方案可能會作出$options一類,所以lengthtest1可以使用默認值和預定義類型的屬性。