Вопрос

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.

Это было полезно?

Решение

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.

Другие советы

According to the PHP Fig standard it's done like below, Though I haven't found an IDE that would type hint this in 2020. :-(

/** 
 * @var array<string,ObjectClass>
 */
private $myAssociativeArray;

https://github.com/php-fig/fig-standards/blob/211063eed7f4d9b4514b728d7b1810d9b3379dd1/proposed/phpdoc.md#collections

There is an open issue for PHPStorm: https://youtrack.jetbrains.com/issue/WI-43843

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top