PHP: Type hints for fields with Eclipse PDT
Asked Answered
N

2

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

Natelson answered 15/4, 2010 at 8:59 Comment(0)
L
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.

Leid answered 31/5, 2010 at 16:4 Comment(4)
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??Rhachis
@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?Leid
@jsalvata : I use Indigo too, and it works for me. I use PDT v3.0.0.v20110516-1100-77--84_23JBVgSVXO7XGJz0VLa9Leid
Working on Neon.2 Release (4.6.2)Letitialetizia
E
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.

Ecstatic answered 12/9, 2012 at 11:30 Comment(4)
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.Ferromagnesian
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.Ecstatic
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.Teleprinter
@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 KeplerMarlenmarlena
L
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.

Leid answered 31/5, 2010 at 16:4 Comment(4)
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??Rhachis
@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?Leid
@jsalvata : I use Indigo too, and it works for me. I use PDT v3.0.0.v20110516-1100-77--84_23JBVgSVXO7XGJz0VLa9Leid
Working on Neon.2 Release (4.6.2)Letitialetizia

© 2022 - 2024 — McMap. All rights reserved.