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

只是好奇 netbeans 中是否有一种方法可以为正则变量提供类型提示,这样智能感知就可以发现它。我知道你可以对类属性、函数参数、返回类型等等这样做,但是我不知道如何对正则变量这样做。如果您有一个可以返回不同对象类型(比如服务定位器)的方法,那么它将非常有帮助。

例如:

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

在之后使用 $somService 时,netbeans 将提供在 Some _ Service 类中定义的所有可用方法。

47787 次浏览

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

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.

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"} */

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

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;

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.