17

How can I specify the type of the keys in the documentation of a PHP method that returns an array?


For example, I use this for an array of objects @return MyClass[].

But I want to comment an array like array( 'string' => array( MyClass ) ), is this possible?

MikO
  • 18,243
  • 12
  • 77
  • 109
  • 1
    @RahilWazir, I'm using `@return MyClass[]` to get code completion in PHPStorm and it works perfectly... Why should I not do it? How could I get correct code completion just saying `@return array`? ***EDIT:*** Also, it is in the official PHPDoc site: http://phpdoc.org/docs/latest/guides/types.html#arrays – MikO Jun 23 '14 at 22:21
  • See this: http://stackoverflow.com/questions/15414103/best-way-to-document-array-options-in-phpdoc I don't know about PHPStorm code completion. But you can comment array keys in the answer given but i don't think it should work for code completion – Rahil Wazir Jun 23 '14 at 22:38
  • Are you using an object as an array key? If not, what autocompletion are you looking for from the array key variable when it's not an object? I must be missing something. – ashnazg Jul 08 '14 at 21:11
  • 2
    There is no settled syntax for documenting the datatype of an array key, so I'm not confident that any IDE has tried to implement a way to derive it. There is a proposed effort to use a "Generics" syntax to formalize a way to document the key's datatype (https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#collections), but I'm not aware that any IDE has tried to implement that either. – ashnazg Jul 30 '14 at 16:08
  • 2
    Possible duplicate of [Comment associative array in PHP Documentor](http://stackoverflow.com/questions/2713710/comment-associative-array-in-php-documentor) – Christian Jan 03 '16 at 04:10

2 Answers2

20

Years after the question, it is possible to document array keys and values using, for example, @return array<integer, string> for an array of string values indexed by integer keys.

I do not know to what extent this syntax is standard and well supported. Tools such as PHPCS or Psalm do support it.

Emmanuel Guiton
  • 1,315
  • 13
  • 24
-16

Why would you need to specify the datatype. You can just access the values in the array by using a foreach to loop trough the results.

For example:

$arr = array("name" => "MikO", "street" => "straatnaam", "number" => "2");


//Here is key the first key, in this case: name, street, number
//And value are the values of the keys, in this case: MikO, straatnaam, 2

foreach($arr as $key => $value)
{
echo $key." ".$value."<br/>";
}

You can also extract some specific data out of it by using this:

echo $arr['name'];

If you want to go into the object:

foreach($arr as $objname => $objcontent)
{
 foreach($objcontent as $objnodename => $objnodevalue)
{
echo $objnodename." = ".$objnodevalue;
}

}
user3734231
  • 40
  • 1
  • 15
  • 11
    Thanks mate, but fortunately I already know how to loop through an array :) What I want is adding proper documentation for that kind of array in order to get code hinting in my IDE, which has nothing to do with your answer... – MikO Jun 24 '14 at 12:18
  • @MikO O sorry, you can just add comments to your code or isn't that good enough? – user3734231 Jun 24 '14 at 13:00
  • 4
    That's probably good enough for (some) humans, but not for PHPStorm, which is not able to understand just text comments and give me code completion... – MikO Jun 26 '14 at 09:54