X

An Oracle blog about NetBeans for PHP

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.



Join the discussion

Comments ( 13 )
  • Varun Thursday, October 9, 2008

    Cool :D


  • Mythos Thursday, October 9, 2008

    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.

    example:

    $foo[0] = new Address();

    $foo[0]->

    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.

    Thanks.


  • hantsy Thursday, October 9, 2008

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


  • Petr Thursday, October 9, 2008

    Ahoj Michale,

    this is a bug. I have entered it as http://www.netbeans.org/issues/show_bug.cgi?id=149636 .

    Thanks,

    Petr


  • Petr Thursday, October 9, 2008

    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.

    Regards,

    Petr


  • cahya Monday, October 13, 2008

    Thank you for your kindness to share a useful article.


  • ramime Wednesday, November 26, 2008

    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 \*/

    $test=$foo->

    but it also doesn't work.

    Any suggestions on this ?


  • Petr Thursday, November 27, 2008

    Hi Ramine,

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

    Regards,

    Petr


  • Carlos Monday, December 1, 2008

    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();

    $tpl->parseBlock("BLOCK_REGIONS",true);

    }

    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.


  • Petr Monday, December 1, 2008

    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.

    Regards,

    Petr


  • manoux Thursday, February 19, 2009

    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 !


  • Paul Tuesday, March 17, 2009

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


  • links of london Thursday, October 29, 2009

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.