I’m fairly new to PHP, and I just started using NetBeans to develop my PHP code.
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.
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?
Adding the type in a
@vartag 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.Edit: 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 theENTERkey. This will expand a phpDoc style comment and add the appropriate tags.Edit 2:
You can use the
@vartag on a property and you can use the@paramtag on a method to achieve the same effect with parameters passed into a method.Use of the
@vartag on a property will give you code hints while using the property any where it is visible:Use of the
@paramtag on a method will give you code hints while using the parameter inside the method:Another way to achieve a similar effect while also providing a modicum of type safety is to use PHP’s type hinting mechanism:
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
@paramtag and PHP will produce aE_RECOVERABLE_ERRORif 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.