Netbeans (PHP) 中的变量类型提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1798477/
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:53:07  来源:igfitidea点击:

Variable type hinting in Netbeans (PHP)

phpphpdocphp-ide

提问by rr.

Just curious if there's a way in netbeans to give type hints for regular variables, so that intellisense picks it up. I know you can do it for class properties, function parameters, return types, etc. but I can't figure out how to do it for regular variables. It's something that would really help in situations where you have a method that can return different object types (like a service locator).

只是好奇 netbeans 中是否有一种方法可以为常规变量提供类型提示,以便智能感知接收它。我知道您可以为类属性、函数参数、返回类型等执行此操作,但我不知道如何为常规变量执行此操作。在您拥有可以返回不同对象类型(如服务定位器)的方法的情况下,这真的很有帮助。

ex something like:

例如:

/**
 * @var Some_Service $someService
 */
$someService = ServiceLocator::locate('someService');

Where using $someService afterward, netbeans would provide all available methods defined in the class Some_Service.

之后使用 $someService 时,netbeans 将提供类 Some_Service 中定义的所有可用方法。

回答by johannes

A single line is all you need:

您只需要一行:

/* @var $varName Type_Name */

See this article in the NetBeans PHP Blog: https://blogs.oracle.com/netbeansphp/entry/defining_a_variable_type_in

请参阅 NetBeans PHP 博客中的这篇文章:https: //blogs.oracle.com/netbeansphp/entry/defining_a_variable_type_in

Note: At least, in version 8.2; The key seems to be:

  • The single asterisk (/*instead of /**).
  • Placing the type after the variable name.
  • Having nothing before and after the type-hinting (except white-space, but even that is not allowed when the comment is not in a single line).

注意:至少在 8.2 版本中;关键似乎是:

  • 单个星号(/*而不是/**)。
  • 将类型放在变量名之后。
  • 在类型提示之前和之后没有任何内容(除了空格,但当注释不在一行中时,即使这样也是不允许的)。

回答by oucil

I know this is an older question, but I was looking for a similar answer for Eclipse/Zend Studio and this solved it as well.

我知道这是一个较旧的问题,但我一直在为 Eclipse/Zend Studio 寻找类似的答案,这也解决了它。

**Note though that it must be on a single line with the opening and closing explicitly in this style...

**请注意,它必须在一行上,并以这种样式明确地打开和关闭...

/* @var $varName Type_Name */

No other formats whether...

没有其他格式是否...

/**
 * @var $varName Type_Name
 */ 

or...

或者...

// @var $varName Type_Name

seemed to work at all. Hope that helps someone.

似乎工作。希望能帮助某人。

回答by GHH

Are you looking to document those pesky magicvariables? (I did; This question currently ranks top result for that in Google. I hope this helps someone!)

你想记录那些讨厌的魔法变量吗?(我做了;这个问题目前在谷歌中排名最高。我希望这对某人有所帮助!)

The @propertytag allows you to document magicphp variables - those implemented using __get()and __set(). The tag should be used in the documentation immediately preceding the class definition:

@property标签允许您记录魔法php 变量 - 那些使用__get()和实现的变量__set()。该标签应该在类定义之前的文档中使用:

/**
 * Class Contact
 * @property string $firstName
 * @property string $lastName
 */
class Contact extends Model {
   ...

This notation triggers autocomplete, tested in Netbeans 8.1 and PhpStorm 2016.1.

此表示法触发自动完成,在 Netbeans 8.1 和 PhpStorm 2016.1 中测试。

enter image description here

在此处输入图片说明

回答by rybo111

According to this bug report, the syntax will change in NetBeans 9:

根据此错误报告NetBeans 9 中的语法将发生变化:

/* @var $variable VarType */    // vdoc1 (legacy syntax)
/** @var VarType $variable */   // vdoc (new syntax)

Also, it's worth mentioning that you can append []to a class name to indicate an array of objects:

此外,值得一提的是,您可以附加[]到类名以指示对象数组:

/* @var $foos Foo[] */
$foos = // ...

foreach ($foos as $foo) {
    // $foo will be hinted as Foo here
}

And don't forget your usestatement, e.g. use Foo;

并且不要忘记您的use陈述,例如use Foo;

回答by Mike

In netbeans 8.0.2, the vdoctemplate gives you this:

在 netbeans 8.0.2 中,vdoc模板为您提供:

/* @var $variable type */

Netbeans will not recognize this however, and will not give you the correct autocomplete list for your objects. Instead use this, just before your variable declaration :

然而,Netbeans 不会识别这一点,并且不会为您的对象提供正确的自动完成列表。而是在变量声明之前使用它:

/** @var objectType $varName */

I have not really seen a great use for the stock vdocTemplate, especially for class variables that are going to be used as PDO or PDOStatement objects.

我还没有真正看到股票vdoc模板的巨大用途,尤其是对于将用作 PDO 或 PDOStatement 对象的类变量。

One solution I use is actually to go into Tools / Options / Editor / Code Templates (with PHP selected as your Language), and add a new Template. I called mine hint. Then under Expanded Text, use the following template:

我使用的一种解决方案实际上是进入工具/选项/编辑器/代码模板(选择 PHP 作为您的语言),并添加一个新模板。我打电话给我的提示。然后在扩展文本下,使用以下模板:

/** @var ${VAR_TYPE variableFromNextAssignmentType default="ClassName"} $$${VARIABLE variableFromNextAssignmentName default="variable"} */