Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Annotation Array (Key, Value)

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?

like image 606
eXe Avatar asked Oct 20 '25 05:10

eXe


2 Answers

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){}    
like image 83
user1842104 Avatar answered Oct 21 '25 20:10

user1842104


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.

like image 24
Georgi Marinov Avatar answered Oct 21 '25 19:10

Georgi Marinov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!