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

发布于 2024-08-12 02:18:11 字数 337 浏览 9 评论 0原文

只是好奇 Netbeans 中是否有一种方法可以为常规变量提供类型提示,以便智能感知能够识别它。我知道你可以对类属性、函数参数、返回类型等执行此操作,但我不知道如何对常规变量执行此操作。当您有一个可以返回不同对象类型(如服务定位器)的方法时,这确实很有帮助。

例如:

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

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

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风启觞 2024-08-19 02:18:11

您只需要一行即可:

/* @var $varName Type_Name */

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

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

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

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

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).
拔了角的鹿 2024-08-19 02:18:11

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

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

/* @var $varName Type_Name */

没有其他格式,无论...

/**
 * @var $varName Type_Name
 */ 

或...

// @var $varName Type_Name

似乎都可以工作。希望对某人有帮助。

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.

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

一张白纸 2024-08-19 02:18:11

您是否想要记录那些讨厌的神奇变量? (我做到了;这个问题目前在 Google 中排名最高。我希望这对某人有帮助!)

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

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

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

输入图像描述这里

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

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

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

enter image description here

只是偏爱你 2024-08-19 02:18:11

根据此错误报告,语法将在NetBeans 9

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

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

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

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

不要忘记您的 use 语句,例如 use Foo;

According to this bug report, the syntax will change in 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 use statement, e.g. use Foo;

花桑 2024-08-19 02:18:11

在 netbeans 8.0.2 中,vdoc 模板为您提供了以下信息:

/* @var $variable type */

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

/** @var objectType $varName */

我还没有真正看到库存 vdoc 模板的巨大用途,特别是对于将用作 PDO 或 PDOStatement 对象的类变量。

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

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

In netbeans 8.0.2, the vdoc template gives you this:

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

/** @var objectType $varName */

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

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:

/** @var ${VAR_TYPE variableFromNextAssignmentType default="ClassName"} $${VARIABLE variableFromNextAssignmentName default="variable"} */
脱离于你 2024-08-19 02:18:11

对于 NetBeans IDE 8.2 语法如下:

class foobar{
    /** @var string $myvar: optional description here **/
    protected static $myvar;
}

这至少会为静态变量正确提供类型提示。

For NetBeans IDE 8.2 syntax is like this:

class foobar{
    /** @var string $myvar: optional description here **/
    protected static $myvar;
}

This will provide the type hints properly for static variables at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文