28

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?

stefmikhail
  • 6,877
  • 13
  • 47
  • 61
  • Can you post an example of what you mean? – Aurelio De Rosa Oct 22 '11 at 17:36
  • 1
    @AurelioDeRosa - Here is a link to the page that describes the feature: http://blogs.oracle.com/netbeansphp/entry/defining_variable_type_in_a – stefmikhail Oct 22 '11 at 17:42
  • Oh...now I understand what you meant. Well, the Jonathan Spooner answer is the right one. – Aurelio De Rosa Oct 22 '11 at 17:43
  • 1
    The general answer is that for auto-complete to work, and since PHP is weakly typed, there is no way for your IDE to know what type a variable is without running the code, which is not viable, to say the least. When editing Java, C or C++ files, this is not necessary because the variable type is defined in its declaration. – netcoder Oct 22 '11 at 17:59
  • Does this answer your question? [How to declare the type for local variables using PHPDoc notation?](https://stackoverflow.com/questions/14462390/how-to-declare-the-type-for-local-variables-using-phpdoc-notation) – Oleg May 08 '21 at 19:17

5 Answers5

39

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.

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 the ENTER key. This will expand a phpDoc style comment and add the appropriate tags.

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.

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;

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) {

}

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) {

}

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.

Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
  • In my case, I have a variable of the name `$clause` and I know by using `gettype($clause)` that the type is `string` yet the autocomplete in NetBeans doesn't have `string` as an option. Is it under a different name in NetBeans? Could you give me an example of the syntax for my case? – stefmikhail Oct 22 '11 at 17:44
  • The var tag will only support code completion for user defined types or classes included in the php lib. See edit for a tip – Jonathan Spooner Oct 22 '11 at 17:47
  • Thanks, but I'm still having problems selecting a type for my variable. Is there documentation somewhere that can describe the types of variables? All the options available in NetBeans are foreign to me, and I'm not sure which to choose. As I said, I used `gettype` to find the variable type for `$clause` as `string`. Yet there is no `string` type as an option. Am I using the wrong method? – stefmikhail Oct 24 '11 at 18:51
  • 1
    The `string` type that you're seeing is what PHP ended up casting the variable to; it is a primitive type and not a class, so there will be no code hinting done in NetBeans. See http://www.php.net/manual/en/language.types.intro.php for a list of the primitive types. See http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.var.pkg.html for the phpDoc @var tag documentation – Jonathan Spooner Oct 24 '11 at 19:28
  • So are you saying that in my case, I won't be able to use the code hinting? Sorry, I'm not completely understanding this feature. If I can use feature, what would I place as the code? – stefmikhail Oct 24 '11 at 19:41
  • Ok, I guess my question should be this: If my variable is *not* a class variable, is this all a moot discussion? – stefmikhail Oct 24 '11 at 19:47
  • Yes, in the case of a `string`, since it's not a class but a primitive, therefore NetBeans has no code hints to provide. It's still a good idea to use the phpDoc to document your intentions, but NetBeans will only provide code hints for user defined or PHP defined classes. – Jonathan Spooner Oct 24 '11 at 19:48
  • I see, so in the end the hint/warning that is popping up is really not applicable to my situation. It's funny because I switched to NetBeans in an attempt to hone my PHP development. Thus far it's only confused me with alerts that either don't apply to my situation, or are incorrect. Last question, can you give me an example of the phpDoc comment that I would use in this case? My hope is that I can extrapolate from that what I should use in future situations. – stefmikhail Oct 24 '11 at 19:56
  • As I've written in the **Edit:** you can use the `/**` expansion feature to let NetBeans determine the types that your methods are returning. Before you do the expansion, be sure and complete your method so NetBeans can make a more accurate guess as to what your type is. I hope this has helped. – Jonathan Spooner Oct 24 '11 at 20:06
15

I guess you're talking about something like that:

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

This is so called phpDoc comment. It allows you to generate API documentation (like this one for instance). Furthermore, most IDEs including Eclipse and NetBeans also support that syntax, and provide dynamic code completion etc.

Crozin
  • 43,890
  • 13
  • 88
  • 135
10

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. 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.

Kat
  • 323
  • 2
  • 8
  • 1
    That is wrong - type should be before variable name: https://docs.phpdoc.org/latest/references/phpdoc/tags/var.html – Oleg Mar 03 '20 at 11:35
  • @Oleg please read more carefully before posting. My answer referred to NetBeans or PhpStorm annotations, not some PHP documentor standards. And it's from 4 years ago so while things might have changed you should've posted a link to NetBeans or PHPStorm docs. You link leads to "page not found". – Kat May 05 '21 at 10:56
  • If the answer is outdated please update it or remove – Oleg May 06 '21 at 16:29
  • PHPStorm appritiates "some" PHP standards https://www.jetbrains.com/help/phpstorm/phpdoc-comments.html#344b8ee0 that are also known as PSR-5: PHPDoc https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#3-definitions – Oleg Jun 03 '21 at 19:45
4

Despite netbeans use it for autocompletion it is often useful for documenting your code:

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

/**
 * 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 */
venimus
  • 5,907
  • 2
  • 28
  • 38
1

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. For example, expecting a mixed type and sending an integer.

Len
  • 542
  • 1
  • 5
  • 11