22

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?

5
  • 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: phpdoc.org/docs/latest/guides/types.html#arrays Commented Jun 23, 2014 at 22:21
  • See this: stackoverflow.com/questions/15414103/… 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 Commented Jun 23, 2014 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. Commented Jul 8, 2014 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 (github.com/phpDocumentor/fig-standards/blob/master/proposed/…), but I'm not aware that any IDE has tried to implement that either. Commented Jul 30, 2014 at 16:08
  • 2
    Possible duplicate of Comment associative array in PHP Documentor Commented Jan 3, 2016 at 4:10

3 Answers 3

30

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.

Sign up to request clarification or add additional context in comments.

Comments

3

You can try to modify the format, it returns an array with special keys(lat, lng) with their types:

@return array{lat: float, lng: float}

Comments

-18

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;
}

}

3 Comments

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 O sorry, you can just add comments to your code or isn't that good enough?
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...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.