JetBrains WebIDE: PHP variable type hinting?

半世苍凉 提交于 2019-11-27 00:09:14

问题


Is there a way to hint WebIDE that a variable has some type? I have to iterate an array of objects, and there's no auto-completion available. This helps in ZendStudio:

/* @var ClassName $object */

I know there's a feature in JetBrains to declare an array of objects:

/**
 * @return ClassName[]
 */

But this works only with function's return type.


回答1:


/* @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.




回答2:


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.



来源:https://stackoverflow.com/questions/1816641/jetbrains-webide-php-variable-type-hinting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!