Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Type hints for fields with Eclipse PDT

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

like image 804
Silvio Donnini Avatar asked Apr 15 '10 08:04

Silvio Donnini


2 Answers

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.

like image 99
Martin Avatar answered Nov 16 '22 20:11

Martin


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.

like image 19
greg0ire Avatar answered Nov 16 '22 21:11

greg0ire