91

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.

Robin van Baalen
  • 3,632
  • 2
  • 21
  • 35
rr.
  • 6,484
  • 9
  • 40
  • 48
  • 1
    This works inside a class, for members, but I don't know how to do it in functions or procedural code. – David Snabel-Caunt Nov 25 '09 at 17:43
  • 1
    I'd think about renaming this to *Variable type hinting in PHP IDEs* as this type of comment should work in all common IDEs (NEtBeans, Eclipse, ...). – shadyyx Nov 14 '13 at 16:21

6 Answers6

191

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).
Top-Master
  • 7,611
  • 5
  • 39
  • 71
johannes
  • 15,807
  • 3
  • 44
  • 57
  • 17
    The key here seems to be the single asterisk /* instead of /**. At least, in version 8.0. Thanks. – Cypher Jun 23 '14 at 17:56
  • 1
    Seems not to work at all if we need to use vdoc for object property, i.e. for `$this->obj = $serviceLocator->get('obj');` if I use `/* @var $obj Obj */` it doesn't work... – shadyyx Feb 05 '15 at 08:56
  • 2
    @shadyyx In this case you should use `/** @var Type_Name */`, see this [post](http://stackoverflow.com/a/9004669/1356425)? – Apostle Feb 06 '15 at 14:52
  • 6
    Remember that you can type `vdoc` on the line above the variable you're going to document and then hit `Tab` and that will act as a macro to automatically create the doc block @johannes posts above. – Tom Auger Mar 04 '15 at 20:29
  • still seeing PHPDoc not found :( – Junaid Atari Sep 15 '15 at 17:50
  • Long shot, but do you know if for the type there's anyway to use a variable? E.g. I have a variable that could be either My_Class_One, or My_Class_Two, initialized like `$class = new $myClass();`, and I'd like to be able to hint like `/* @var $class $myClass */` – TMH Jan 06 '16 at 12:33
  • Sorry to update the old topic, but does anyone know if it's possible to `vdoc` multiple variables in a single comment? 'couse a lot of consequed comment lines like `/* @var ... ... */ /* @var ... ... */` doesn'n look good when i'm pointing the incoming variables which are defined in another file. – Denis Alexandrov Jan 20 '16 at 10:17
  • 1
    If you want NetBeans to support alternative syntaxes as well, please comment/vote on the feature request: https://netbeans.org/bugzilla/show_bug.cgi?id=267470 – marcovtwout May 23 '17 at 14:17
  • Visual Studio Code syntax. `/** @var ClassName $yourVaribleName */` Works single and multiline. – DevWL Nov 30 '18 at 06:28
24

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.

oucil
  • 4,211
  • 2
  • 37
  • 53
  • Using the double slash method listed last did not work for me in NetBeans 7.2 – David Feb 26 '13 at 21:48
  • 1
    @David It may not read that well, but I did say that ONLY the first works. Although both of the others are valid comments, neither of them work with the type hinting system, at least as far as eclipse goes, not sure about NetBeans. – oucil Feb 27 '13 at 04:00
  • 1
    Netbeans (8.01) only accepts the first option, using /*, but phpStorm (8) supports /** as well. – Muundruul Nov 10 '14 at 13:39
11

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

GHH
  • 761
  • 7
  • 13
5

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;

rybo111
  • 12,240
  • 4
  • 61
  • 70
2

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"} */
Mike
  • 68
  • 6
  • 3
    Hm, I have NB 8.0.2 and for me it's the other way around. The /* @var $variable type */ works fine. I did try to use your patch for the Templates. but it didn't work. – userfuser Oct 08 '15 at 11:28
  • 1
    I'm not sure exactly what was going on with my copy of netbeans when I first posted this answe, but I'm using 8.2 now. /* @var $varName varType */ works just fine. – Mike Nov 13 '17 at 19:06
0

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.

andreszs
  • 2,896
  • 3
  • 26
  • 34