简体   繁体   English

为什么在注释中声明PHP变量类型?

[英]Why declare PHP variable type in a comment?

I'm fairly new to PHP, and I just started using NetBeans to develop my PHP code. 我刚接触PHP,所以刚开始使用NetBeans开发我的PHP代码。

Out of the blue, as I entered a variable in a query, a dialog popped up and asked me to complete a comment to hold the variable type. 出乎意料的是,当我在查询中输入变量时,弹出一个对话框,要求我完成注释以保存变量类型。 I did some investigation and found that this seems to be a popular feature of NetBeans, but I couldn't find any information to explain to me why this was the case. 我进行了一些调查,发现这似乎是NetBeans的流行功能,但是我找不到任何信息可以向我解释为什么是这种情况。

Why would someone want to place a PHP variable's type in a comment? 为什么有人要在注释中放置PHP变量的类型? Is it for development use, or does it actually benefit the code itself? 它是供开发使用,还是实际上使代码本身受益? Is it integral, or optional? 它是不可或缺的,还是可选的?

Adding the type in a @var tag inside your method's comment will allow NetBeans to show you code completion. 在方法注释内的@var标记中添加类型将使NetBeans可以显示代码完成。 This of course is optional but it is always a good idea to fully document your code. 这当然是可选的,但是完全记录您的代码始终是一个好主意。

Edit: A tip for NetBeans to auto-generate the comments for you is to use the /** expansion. 编辑: NetBeans为您自动生成注释的提示是使用/**扩展。 To do this, simply place the cursor above the property or method you want to document and type /** and then press the ENTER key. 为此,只需将光标放在要记录的属性或方法上方,然后键入/** ,然后按ENTER键。 This will expand a phpDoc style comment and add the appropriate tags. 这将扩展phpDoc样式的注释并添加适当的标签。

Edit 2: You can use the @var tag on a property and you can use the @param tag on a method to achieve the same effect with parameters passed into a method. 编辑2:可以在属性上使用@var标记,并且可以在方法上使用@param标记,以通过传递给方法的参数实现相同的效果。

Use of the @var tag on a property will give you code hints while using the property any where it is visible: 在属性上使用@var标记将在使用该属性的任何可见位置为您提供代码提示:

/**
 *
 * @var My_Type
 */
private $_myProperty;

Use of the @param tag on a method will give you code hints while using the parameter inside the method: 在方法上使用@param标记将在使用方法内部的参数时为您提供代码提示:

/**
 *
 * @param My_Type $obj 
 */
public function myMethod($obj) {

}

Another way to achieve a similar effect while also providing a modicum of type safety is to use PHP's type hinting mechanism: 在提供少量类型安全性的同时实现相似效果的另一种方法是使用PHP的类型提示机制:

public function myMethod(My_Type $obj) {

}

Notice that this method has the type specified in the method signature. 请注意,此方法具有方法签名中指定的类型。 NetBeans will now provide the same code completion inside the method that is available using the @param tag and PHP will produce a E_RECOVERABLE_ERROR if the type passed into the method is not the same type that was specified. 现在,NetBeans将在使用@param标记可用的方法内提供相同的代码完成,并且如果传递给该方法的类型与指定的类型不同,PHP将产生E_RECOVERABLE_ERROR See PHP's documentation regarding errors and how to handle them if your interested in learning more about the above error. 如果您想了解有关上述错误的更多信息,请参见有关错误以及如何处理错误的PHP文档

I guess you're talking about something like that: 我猜你在说这样的话:

/**
 * @var SimpleXMLElement $xml
 */
private $xml;

This is so called phpDoc comment . 这就是所谓的phpDoc comment It allows you to generate API documentation ( like this one for instance ). 它允许您生成API文档( 例如,类似于此文档)。 Furthermore, most IDEs including Eclipse and NetBeans also support that syntax, and provide dynamic code completion etc. 此外,包括Eclipse和NetBeans在内的大多数IDE也支持该语法,并提供动态代码完成等功能。

If you want to declare the type of a variable in the case where the variable is not a class property but just a variable that holds some returned value, use single star comments followed by @var followed by your variable name and finally followed by the type of that variable . 如果要在变量不是类属性而是仅包含一些返回值的变量的情况下声明变量的类型,请使用单星注释,后跟@var,后跟变量名,最后是类型那个变量 For example: 例如:

/* @var $errorMessage NotificationMessage */
$errorMessage= $allMessages->rewind()->current();

will tell NetBeans or PhpStorm that $errorMessage is an instance of NotificationMessage, and you should get the code completion for that variable. 会告诉NetBeans或PhpStorm $ errorMessage是NotificationMessage的实例,您应该获取该变量的代码完成。

Despite netbeans use it for autocompletion it is often useful for documenting your code: 尽管netbeans使用它进行自动补全,但通常对于记录代码还是很有用的:

In this case you know what this method gets and what it returns but inside the code you have no idea what is happening 在这种情况下,您知道此方法得到什么以及它返回什么,但是在代码内部您不知道发生了什么

/**
 * Returns some stuff
 * @param string $someObj
 * @return array
 */
public function someMethod($someObj) {
    $factoredObj = getObject($someObj); //you are not sure what type it returns
    $resultArray = $factoredObj->toArray();
    return $resultArray;
}

You can comment it with /* @var $variable type */ inside the code 您可以在代码中使用/* @var $variable type */对其进行注释

/**
 * Returns some stuff
 * @param string $someObj
 * @return array
 */
public function someMethod($someObj) {
    /* @var $factoredObj someType */
    $factoredObj = getObject($someObj);
    $resultArray = $factoredObj->toArray();
    return $resultArray;
}

or 要么

$factoredObj = getObject($someObj); /* @var $factoredObj someType */

Because PHP is a loose/duck typed language, when you create a large program those type hints can help you or others understand what is going on if an issue should arise. 因为PHP是一种松散/鸭式语言,所以当您创建大型程序时,这些类型提示可以帮助您或其他人了解如果出现问题的情况。 For example, expecting a mixed type and sending an integer. 例如,期望混合类型并发送整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM