JetBrains WebIDE:PHP 变量类型提示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1816641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:56:41  来源:igfitidea点击:

JetBrains WebIDE: PHP variable type hinting?

phpphpstormtype-hintingweb-ide

提问by kolypto

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:

有没有办法提示 WebIDE 变量具有某种类型?我必须迭代一个对象数组,并且没有可用的自动完成功能。这有助于 ZendStudio:

/* @var ClassName $object */

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

我知道 JetBrains 中有一个功能可以声明一个对象数组:

/**
 * @return ClassName[]
 */

But this works only with function's return type.

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

回答by Alexey Gopachenko

/* @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 */是无效的 PHPDOC 注释,不会在当前版本的 Web IDE 中解析。使用双星号使其工作:

/** @var ClassName $object */

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

此外,您可以$arrayforeach($array as $var)with 中进行注释/** @var ClassName[] $array */$var类型将被自动推导。

回答by álvaro González

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

正如已经指出的,PhpStorm 将使用常规的 phpdoc 块:

/** @var ClassName $object */

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

但是,从2.1 开始,它还支持 Netbeans/Eclipse/Zend @var 注释

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

请注意注释以/*而不是开头/**(因此如果您使用 phpdoc 生成实际文档,它将不会显示)。此外,参数被交换,尽管 PhpStorm 接受任何顺序:

/* @var ClassName $object */

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

最后但并非最不重要的一点是,它们几乎可以在任何任意代码行之前(从技术上讲,phpdoc 块仅限于某些项目)。



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.

编辑:截至 2019 年,Netbeans/Eclipse/Zend @var 注释似乎大多被放弃。NetBeans 11 不再支持它们,并且通常其他 IDE 不支持它们。我建议使用其他语法。