JetBrains WebIDE: PHP 变量类型提示?

有没有一种方法可以提示 WebIDE 变量具有某种类型? 我必须迭代一个对象数组,而且没有可用的自动完成。 这对 ZendStudio 很有帮助:

/* @var ClassName $object */

我知道 JetBrains 有一个声明对象数组的特性:

/**
* @return ClassName[]
*/

但是这只适用于函数的返回类型。

46558 次浏览

/* @var ClassName $object */ is a non-valid PHPDOC comment and is not parsed in the current version of Web IDE. Use double asterisks to make it work:

/** @var ClassName $object */

Also, you can annotate $array in foreach($array as $var) with /** @var ClassName[] $array */ and $var type will be deduced automatically.

As already pointed out, PhpStorm will use regular phpdoc blocks:

/** @var ClassName $object */

However, since 2.1 it also supports Netbeans/Eclipse/Zend @var annotations:

/* @var $object ClassName */

Please note the comment starts with /* rather than /** (thus it won't show up if you generate actual documentation with phpdoc). Also, the arguments are swapped, though PhpStorm accepts any order:

/* @var ClassName $object */

Last but not least, they can precede almost any arbitrary line of code (technically, phpdoc blocks are restricted to certain items).


Edit: as of 2019, Netbeans/Eclipse/Zend @var annotations seem to be mostly abandoned. NetBeans 11 no longer supports them and in general they aren't supported by other IDEs. I suggest to use the other syntax.