4

I work with Netbeans 8.0.2.

Is there a way to declare (and with autocomplete usage of course) array key and value types of an @return array annotation?

Like:

@return array[string]Class2

Or like:

@return Class2[string]

So Netbeans should have no problems with autocomplete on following foreach:

foreach($aArray as $sString => $oClass2){ ... }

I know about the following way of annotation:

@return Class2[]

But in this way I have no clue how to get the autocomplete on the string key.

Sure, "String" - there is no autocomplete, but let's say we want to add another Object instead a string as Key, then how could I inform my IDE to let it know and to get the right autocomplete?

2 Answers 2

2

As far as I know the key can't be typehinted, but you can typehint the value in foreach loops like this:

/**
 * $var $value MyTypeHint
 */
foreach($array as $key => $value){}    
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, that would be a way. But i can't imagine that there is no possible annotation of an array or an multidimensional array - that would be pretty sad :-/
The only time I'm aware of where you might have an object as the key would be in SplObjectStorage (in arrays you can only have a string or integer as a key). It's probably just too rare a use case for them to justify the effort.
1

I am using netbeans and this way work good:

/* @var $data['a'] \SomeClass */
/* @var $data['b'] string */
$data = array(
  "a" => new \SomeClass(),
  "b" => "Dump string"
);

It is important to use single * when make this type of comments in netbeans.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.