Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable type hinting in Netbeans (PHP)

Just curious if there's a way in netbeans to give type hints for regular variables, so that intellisense picks it up. I know you can do it for class properties, function parameters, return types, etc. but I can't figure out how to do it for regular variables. It's something that would really help in situations where you have a method that can return different object types (like a service locator).

ex something like:

/**  * @var Some_Service $someService  */ $someService = ServiceLocator::locate('someService'); 

Where using $someService afterward, netbeans would provide all available methods defined in the class Some_Service.

like image 248
rr. Avatar asked Nov 25 '09 17:11

rr.


People also ask

Should I use Type hinting in PHP?

Apparently 'type hinting' in PHP can be defined as follows: "Type hinting" forces you to only pass objects of a particular type. This prevents you from passing incompatible values, and creates a standard if you're working with a team etc.

What is Type hinting in PHP with example?

Type hinting is a concept that provides hints to function for the expected data type of arguments. For example, If we want to add an integer while writing the add function, we had mentioned the data type (integer in this case) of the parameter.


2 Answers

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).
like image 188
johannes Avatar answered Oct 01 '22 22:10

johannes


I know this is an older question, but I was looking for a similar answer for Eclipse/Zend Studio and this solved it as well.

**Note though that it must be on a single line with the opening and closing explicitly in this style...

/* @var $varName Type_Name */ 

No other formats whether...

/**  * @var $varName Type_Name  */  

or...

// @var $varName Type_Name 

seemed to work at all. Hope that helps someone.

like image 24
oucil Avatar answered Oct 01 '22 23:10

oucil