[英]Variable type hinting in Netbeans (PHP)
Just curious if there's a way in netbeans to give type hints for regular variables, so that intellisense picks it up.只是好奇 netbeans 中是否有一种方法可以为常规变量提供类型提示,以便智能感知接收它。 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.之后使用 $someService 时,netbeans 将提供类 Some_Service 中定义的所有可用方法。
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请参阅 NetBeans PHP 博客中的这篇文章: https : //blogs.oracle.com/netbeansphp/entry/defining_a_variable_type_in
Note: At least, in version 8.2;注意:至少在 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).在类型提示之前和之后没有任何内容(除了空格,但当注释不在一行中时,即使这样也是不允许的)。
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.我知道这是一个较旧的问题,但我一直在为 Eclipse/Zend Studio 寻找类似的答案,这也解决了它。
**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.希望能帮助某人。
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()
. @property
标签允许你记录神奇的php 变量——那些使用__get()
和__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.此表示法触发自动完成,在 Netbeans 8.1 和 PhpStorm 2016.1 中测试。
According to this bug report , the syntax will change in NetBeans 9 :根据此错误报告, 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, eg use Foo;
并且不要忘记您的use
语句,例如use Foo;
In netbeans 8.0.2, the vdoc template gives you this:在 netbeans 8.0.2 中, vdoc模板为您提供:
/* @var $variable type */
Netbeans will not recognize this however, and will not give you the correct autocomplete list for your objects.然而,Netbeans 不会识别这一点,并且不会为您的对象提供正确的自动完成列表。 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.我还没有真正看到股票vdoc模板的巨大用途,尤其是对于将用作 PDO 或 PDOStatement 对象的类变量。
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.我使用的一种解决方案实际上是进入工具/选项/编辑器/代码模板(选择 PHP 作为您的语言),并添加一个新模板。 I called mine hint .我打电话给我的提示。 Then under Expanded Text, use the following template:然后在扩展文本下,使用以下模板:
/** @var ${VAR_TYPE variableFromNextAssignmentType default="ClassName"} $$${VARIABLE variableFromNextAssignmentName default="variable"} */
For NetBeans IDE 8.2 syntax is like this:对于 NetBeans IDE 8.2 语法是这样的:
class foobar{
/** @var string $myvar: optional description here **/
protected static $myvar;
}
This will provide the type hints properly for static variables at least.这将至少为静态变量提供正确的类型提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.