Variable Type Hinting in Netbeans (Php)

Variable type hinting in Netbeans (PHP)

A single line is all you need:

/* @var $varName Type_Name */

See this article in the NetBeans PHP Blog: https://blogs.oracle.com/netbeansphp/entry/defining_a_variable_type_in

Note: At least, in version 8.2; The key seems to be:

  • The single asterisk (/* instead of /**).
  • Placing the type after the variable name.
  • Having nothing before and after the type-hinting
    (except white-space, but even that is not allowed
    when the comment is not in a single line).

Netbeans property type hinting

I figured it out. The answer was actually pretty damn obvious. (blush)

The correct syntax is the same as for any PHPDoc:

/**
* @var array
*/
private $parameters;

I'm surprised nobody managed to point this out in the 5 hours between me asking this question and me discovering the answer myself. Oh well.

Netbeans type-hinting inside of an include

You need to define the type of $_bar in the class

  class foo()
{
/**
* @var Bar
*/
private $_bar;

public function __construct($bar)
{
$this->_bar = $bar;
}

public function doStuff()
{
include('doStuffCode.php');
}
}

And then in your included file you hint:

/* @var $this foo */

Which will be the correct way of doing this. All though I'm not sure private variables can be hinted this way due to the scope of the included file contents.

Defining variable type in Netbeans PHP

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

PHP Type Hinting for a map (associative array) in NetBeans?

The best way I've been able to achieve this is once you begin to iterate through the associative array you can type hint the variable at that point. This will only work if the array holds all of the same class types.

<?php
foreach($myAssociativeArray as $item){
/* @var $item ObjectClass */
Some code here...
}

This should properly pass the ObjectClass type hinting to $item. Again, if you're array holds multiple class types then this solution will not work.

Add Dynamic @return code hinting in Netbeans php

Similar question has been answered here:
Dynamic return type with type hint in PHP?

any kind of code hinting

Yes, try this.

$usr = $this->load('user');/* @var $usr user */

You may wanna use shortcut "vdoc" and press {tab} in the code to generate the type-hinting comment.



Related Topics



Leave a reply



Submit