Home » Php » Defining variable type in Netbeans PHP

Defining variable type in Netbeans PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have found a way to say netbeans type of variable in such way:

/* @var $variablename Type */

However in this case there are no hints (Database is my class):

 //model.php
 abstract class Model {
      /* @var $db Database */
      protected $db;
      (...)
 }

 //Mymodel.php
 class MyModel extends Model {
      (...)
       $this->db-> //no hints
      (...)
 }

Is it Netbeans limit or rather my mistake?

How to&Answers:

First of all, define the variable type first, like this:

/* @var Database $db This is my Database object */

And secondly I would suggest to use phpdoc commenting, like:

class Model {

/**
 * @var Database $db This is my Database object
 */
protected $db;

Should have no issues then…

Answer:

NetBeans can make use of two similar yet different comment annotations:

  1. Good old phpdoc block comments, that start with /** and are placed right before the item definition:

    /**
     * @var Database $db Database connection instance
     */
    protected $db;
    
  2. Variable type inline comments, that start with /* and are placed somewhere before the item use:

    $foo = $this->db;
    /* @var $foo Database*/
    $foo->...
    

The second type comes in handy when docblock comments are either not available or not helpful, e.g. you are using a third-party library that isn’t documented or your variable type cannot be tracked automatically.

You were basically using syntax for #2 in the context for #1 😉