PHP:使用Eclipse PDT为字段键入提示

时间:2023-01-17 19:16:31

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.

使用Eclipse + PDT,我知道您可以通过类型提示指定方法的返回类型或方法中变量的类型。

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?

有没有办法用Eclipse和PDT实现类字段的自动完成?

thanks,

Silvio

2 个解决方案

#1


18  

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

就在这里!只需将var类型放在声明之前,如下所示:

/**
 * @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.

确保使用javadoc样式注释(/ **,而不仅仅是/ *)我通过选择“Outline”视图中的字段找到了这个,然后右键单击> Source> Generate element comment。

#2


19  

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.

如果使用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.

请注意,我们需要在一行中输入一个星号/ *。声明应在使用变量之前进行。

#1


18  

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

就在这里!只需将var类型放在声明之前,如下所示:

/**
 * @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.

确保使用javadoc样式注释(/ **,而不仅仅是/ *)我通过选择“Outline”视图中的字段找到了这个,然后右键单击> Source> Generate element comment。

#2


19  

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.

如果使用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.

请注意,我们需要在一行中输入一个星号/ *。声明应在使用变量之前进行。