Code completion for a class field

NetBeans are able to offer code completion for class fields of a PHP class. Because PHP is not a strict typed language, you can help to NetBeans to find out the type of a field.

Let's have a simple example: 

The example doesn't show good practice, because the class fileds shouldn't be accessible outside the class, but for explanation it's enough.

One way how NetBeans are trying to find out the type of a field is that it looks through the class constructor. In our case in constructor there is expression that assigns new Address object to the $address field. So NetBeans supposes that the field is Address type and code completion for $this->address-> suggests $street and $city fields from Address object.

This functionality was added recently and works only in the file, where the class is defined and still there is a space for improvements.

The second way is to specify the type of a field through PHP Doc. It's more universal, the field doesn't have to be initialized in the constructur. As you can see on the picture, the type is also displayed in the code completion window.  NetBeans helps to write the PHP Doc,  write /\*\* one line above the field and press ENTER. Then NetBeans will generate appropriate PHP Doc. Also you can use code completion after @var tag to obtain all available types from the project in the code completion.


Cool :D

Posted by Varun on October 09, 2008 at 03:34 AM CEST #

Every improvement in code completion is great and I thak you for it, but what I would really appreciate is code completion for array of objects.

$foo[0] = new Address();

In this case NetBeans wrongly assume that variable $foo is an instance of class Address, not an array of instances.

If this should work and I'm doing something wrong, please tell me.


Posted by Mythos on October 09, 2008 at 03:48 AM CEST #

This plugin need more effort.I think it will be a competitor to Zend Studio 6.1. Many OOP feature is not available now.
Generate phpdoc .
Basic refactor.
Quick Class hierarchy and members.
PHP-java bridge support to call java in PHP file.
UML code generation support.
Generate CRUD from database (through a framework support?)
Popular frameworks support(CakePHP,Prado,ZendFramework).

Posted by hantsy on October 09, 2008 at 05:06 AM CEST #

Ahoj Michale,

this is a bug. I have entered it as .


Posted by Petr on October 09, 2008 at 08:33 AM CEST #

Hi hantsy,

thanks for your comment, NetBeans 6.5 will contain the first version of the PHP support. We will add more in the next releases. Stay tuned.


Posted by Petr on October 09, 2008 at 08:42 AM CEST #

Thank you for your kindness to share a useful article.

Posted by cahya on October 13, 2008 at 10:47 AM CEST #

How can I add a type hint for a class if i had code like this:

/\* foo is a instance of class bar declared outside of this file \*/

$test=$foo-> <here i don't get a "no suggestion">

I've tried:
/\*\* @var $foo bar \*/

but it also doesn't work.

Any suggestions on this ?

Posted by ramime on November 26, 2008 at 03:46 PM CET #

Hi Ramine,

unfortunately this is missing in 6.5. It will be available in the next version and in dev buils soon.


Posted by Petr on November 27, 2008 at 05:41 AM CET #

It's such a pity, I've changed from Zend do Eclipse, and now got NB 6.5 and was about to change to it here, but we use lots of codes like:

foreach(PriceList::getAllNotBase() as $priceList){
/\* @var $priceList PriceList \*/
$tpl->REGION_NAME = $priceList->getName();

And without completion (PriceList::getAllNotBase() returns an array of priceList Objects) it's just impossible to me to change IDE.

Congrats for the awesome job, it's so damn good. I'm just waiting for this feature, after a weekend of evaluation, it's the only one that is missing to me.

Posted by Carlos on December 01, 2008 at 11:23 AM CET #

Hi Carlos,

you are right, this is missing in NetBeans 6.5. For the next version we are going to implement this and I hope that it will be available in a nightly build soon.


Posted by Petr on December 01, 2008 at 02:56 PM CET #

Very great tool. I'm leaving ecplise+pdt for this one. ( lot of instability with Ganymede, and no one talks about )

I second Varun for the ability to autocomplete values of array of objects in a loop !

Posted by manoux on February 19, 2009 at 07:47 AM CET #

Where do we look to find out when this is implemented in the nightly build?

Posted by Paul on March 17, 2009 at 12:23 PM CET #

It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

Posted by links of london on October 29, 2009 at 08:23 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed

This blogs is written by NetBeans developers who contribute to the PHP support mainly.


« May 2016