12

Using Eclipse + PDT, I know that you can specify the return type of a method or the type of a variable within a method via type hints.

How about class fields? Can I declare the type of a field in order to enable autocompletion for that variable?

I tried something on the lines of:

class MyClass {

  protected $Field; /* @var $Field MyType */

  ...

but it doesn't work.

Is there a way to achieve autocompletion of class fields with Eclipse and PDT?

thanks,

Silvio

hakre
  • 193,403
  • 52
  • 435
  • 836
Silvio Donnini
  • 3,233
  • 2
  • 28
  • 29

2 Answers2

20

And if you need it for a non-declared local variable you can use

/* @var $varname vartype */

This is very useful if you iterate over an array of objects with a foreach.

Please note that we need to type it with one asterisk /* and all in one line. Declaration should be placed before the use of the variable.

Martin
  • 1,889
  • 3
  • 20
  • 25
  • I cannot get this to work consistently. It does work sometimes - but other times it just doesn't. I cannot seem to find a pattern for it. – Erick Robertson Feb 20 '13 at 13:15
  • It works very well for me with my own class names. Do you have opened more than one project at the same time? This sometimes confuses my eclipse. – Martin Jun 10 '13 at 11:28
  • Great feature! Though, I'd really like to know why they chose to use the single asterisk version, have a different order of name and type than `@param` or the _other_ `@var` and also require the name even if you put it right before the variable... but, well, maybe that's just to _not_ confuse PHP programmers with too much consistency. – Simon Lehmann Sep 11 '13 at 12:38
  • @ErickRobertson : if you just declared a variable of a never-before-used type, and only added a type hinting in a comment, the required `use` declaration is probably not there, and will **not** be automatically added from the comment; and without that, PDT can't make the hint work. **Workaround**: outside the comment, type `ClassName` and hit Ctrl-Space. Eclipse will add "::" to indicate it has recognized it as a class, *and* has added the `use` declaration. Now erase the line. The declaration will stay. And the comment will work. *Tested just now on Eclipse Kepler* – LSerni Jun 20 '14 at 08:39
19

Yes there is! Just simply put the var type before the declaration, like this :

/**
 * @var Type
 */
 protected $Field;

Make sure you use javadoc style comments (/** , not just /* ) I found this by selecting the field in the "Outline" view, and then right-click > Source > Generate element comment.

greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • This works for me, but I don't see the "Source > Generate" section. Is there some other eclipse plugin I need installed to see that?? – Matt Connolly May 05 '11 at 03:56
  • @Matt Connolly: I installed PDT all in one and I have it, no special additional plugin. What entries do you see when you right click on an element of the Outline view? – greg0ire May 05 '11 at 07:46
  • @jsalvata : I use Indigo too, and it works for me. I use PDT v3.0.0.v20110516-1100-77--84_23JBVgSVXO7XGJz0VLa9 – greg0ire Jan 12 '12 at 13:03
  • Working on Neon.2 Release (4.6.2) – Diogo Alves Mar 10 '17 at 15:16