Skip to content
Advertisement

PHP Type Hinting for a map (associative array) in NetBeans?

I’m trying to get advantage of NetBeans’ intelligent way of handling object types, so I’m hinting to every object’s type in comments.

Problem is, I want to hint to an associative array of (string => ObjectClass).

I’ve tried all the followings but nothing worked :

/** 
 * @var [string => ObjectClass]
 */
private $myAssociativeArray;

And

/** 
 * @var string|ObjectClass[]
 */
private $myAssociativeArray;

How can I get NetBeans to know that I’m hinting about a map of string to ObjectClass ?

Thanks in advance.

Advertisement

Answer

The best way I’ve been able to achieve this is once you begin to iterate through the associative array you can type hint the variable at that point. This will only work if the array holds all of the same class types.

<?php
    foreach($myAssociativeArray as $item){
       /* @var $item ObjectClass */
       Some code here...
    }

This should properly pass the ObjectClass type hinting to $item. Again, if you’re array holds multiple class types then this solution will not work.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement