为什么在注释中声明PHP变量类型? [英] Why declare PHP variable type in a comment?

查看:421
本文介绍了为什么在注释中声明PHP变量类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP还是很陌生,我刚开始使用NetBeans开发我的PHP代码.

I'm fairly new to PHP, and I just started using NetBeans to develop my PHP code.

出乎意料的是,当我在查询中输入变量时,弹出一个对话框,要求我完成注释以保存变量类型.我进行了调查,发现这似乎是NetBeans的流行功能,但我找不到任何信息向我解释为什么.

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.

为什么有人要在注释中放置PHP变量的类型?它是供开发使用,还是实际上使代码本身受益?是完整的还是可选的?

Why would someone want to place a PHP variable's type in a comment? Is it for development use, or does it actually benefit the code itself? Is it integral, or optional?

推荐答案

在方法注释内的@var标记中添加类型将使NetBeans可以向您显示代码完成.这当然是可选的,但是完全记录您的代码始终是一个好主意.

Adding the type in a @var tag inside your method's comment will allow NetBeans to show you code completion. This of course is optional but it is always a good idea to fully document your code.

编辑:NetBeans为您自动生成注释的技巧是使用/**扩展.为此,只需将光标放在要记录的属性或方法上方,然后键入/**,然后按ENTER键.这将扩展phpDoc样式的注释并添加适当的标签.

A tip for NetBeans to auto-generate the comments for you is to use the /** expansion. To do this, simply place the cursor above the property or method you want to document and type /** and then press the ENTER key. This will expand a phpDoc style comment and add the appropriate tags.

修改2: 您可以在属性上使用@var标记,并且可以在方法上使用@param标记,以将参数传递给方法来达到相同的效果.

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.

在属性上使用@var标记将在使用该属性的任何可见位置为您提供代码提示:

Use of the @var tag on a property will give you code hints while using the property any where it is visible:

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

在方法中使用@param标记将在使用方法内部的参数时为您提供代码提示:

Use of the @param tag on a method will give you code hints while using the parameter inside the method:

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

}

另一种实现相似效果同时提供类型安全性的方法是使用PHP的

Another way to achieve a similar effect while also providing a modicum of type safety is to use PHP's type hinting mechanism:

public function myMethod(My_Type $obj) {

}

请注意,此方法具有方法签名中指定的类型.现在,NetBeans将在使用@param标记可用的方法内提供相同的代码完成,并且如果传递给该方法的类型与指定的类型不同,则PHP将产生E_RECOVERABLE_ERROR.请参阅有关错误的PHP文档以及如何处理如果您有兴趣进一步了解上述错误,请咨询他们.

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. See PHP's documentation regarding errors and how to handle them if your interested in learning more about the above error.

这篇关于为什么在注释中声明PHP变量类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆