Generate Constructor, Getters and Setters

Posted by Petr Pisl on Jul 21 2008, 03:36:32 PM CEST

This morning I have committed a feature, which offers generating of constructor, getters and setters in a PHP class. To invoke the functionality, the caret position has to be inside a PHP class and you have to press shortcut ALT+Insert (CTLRL+I on Mac). After invoking the shortcut, all possible generators are offered.

Let's explain on a simple example. I have very simple PHP class, where I have only two properties - $name and $age. At first I want to add constructor. After pressing ALT+Insert (CTLRL+I on Mac), the constructor generator is offered - Constructor... item in the Generate menu. After invoking this item, the Generate Constructor dialog is displayed. There I can choose parameters of the constructor. It is possible to chose any property, then the constructor is generated without any parameter and empty. If I check / uncheck the User class, then all properties are selected / unselected. For my demonstration I have chose $name property.  After pressing OK button, the constructor with one parameter is generated.

Now I want to generate getter for the $name. After invoking Generate menu, you can notice that the Constructor... item is not offer anymore, because the class already has a constructor. I choose Getter... item and Generate Getters dialog is displayed. In the dialog you can choose, for which properties you want to generate getters. Again you can check / uncheck the class node, which selects / unselects all properties.

As the next step I want to generate getter and setter for $age property. After choosing Getter and Setter... item in Generate menu, the dialog offers only $age property, because only this property has neither getter nor setter.

When I invoke Generate menu after creating getter and setter for $age property, only setter generator is offered, because there is not defined only setter for $name property.

The generate functionality is designed that you can work just with the keyboard and you don't have to use mouse.

Category: Features | Permalink | Comments [16]

Trackback URL: http://blogs.sun.com/netbeansphp/entry/generate_constructor_getters_and_setters
Comments:

This is really great... And all of this is for free? No way ;)
Keep it up!

Posted by Azrael Valedhel on July 22, 2008 at 01:28 AM CEST #

Super!!! Like in Java. Thanks!!

Posted by Alexius on July 22, 2008 at 05:08 AM CEST #

OH YES, i am waiting so long for this feature.
when goes it online ?

must i wait for nightly build or an extra plugin ?!

Posted by Guido on July 22, 2008 at 08:40 AM CEST #

This functionality is a part of regular build. You can try it in nightly build or continual build.

Unfortunately there is a regression in explorer from last week. Guys from core team work on it, this is P1 (http://www.netbeans.org/issues/show_bug.cgi?id=140230). The regression influence mainly creating new files - there are not visible in the explorer and you have to close and open project again.

Regards, Petr

Posted by Petr on July 22, 2008 at 09:11 AM CEST #

How does it handle PHP4 where there is no __construct or __destruct? Can you set a language version so you don't accidentally use something that won't work?

I won't be hurt to hear that you can't. I'd much prefer to be coding PHP5 but I can't.

Posted by Courtney Miles on July 23, 2008 at 01:52 AM CEST #

Hi Courtney,

NetBeans 6.5 is not going to have direct support for PHP4. There is not possibility to change language specification. By default the IDE support PHP5. You can use it for PHP4 development, there shouldn't be any problem, but some features are written for PHP5.

Thanks,
Petr

Posted by Petr on July 23, 2008 at 11:14 AM CEST #

How do I invoke Alt+Insert on a Mac keyboard? There is no Insert key.

Posted by Rus on July 23, 2008 at 02:54 PM CEST #

On Mac the short cut is CTRL+I.

Regards,
Petr

Posted by Petr on July 23, 2008 at 03:05 PM CEST #

How can one navigate these menus without mouse/arrow-keys?

Posted by Sebastian on August 01, 2008 at 10:50 AM CEST #

Hi Sebastian,

use Up and Down arrows for navigation in the dialogs, space for select/unselect properties and enter for pressing OK button.

Thanks,
Petr

Posted by Petr on August 01, 2008 at 03:57 PM CEST #

Hi. I have NetBeans Ide Dev 200807040101, and i can't see this feature. Is available on this version?

Thanks,
Seb.

Posted by Sebastian on August 11, 2008 at 01:40 PM CEST #

Hi,

Is there any code generation/completion in the way of includes/require similar to the "fix imports" function in the java environment?

What about refactoring for include/require (auto update include statements when a files are moved)?

Kevin

Posted by Kevin Bond on August 12, 2008 at 07:35 PM CEST #

Hi Kevin,

there was similar functionality to auto import in java. But this functionality didn't work as we expected so we removed it until it will be more stabilized.

Also many projects use autoload php functionality, where such feature is not expected. It has to be accessible via a user option.

Thanks,
Petr

Posted by Petr on August 13, 2008 at 12:36 PM CEST #

Is it possible to operate the automatic completion in a "sub" object.

Example :

$this->var1 = new ClassXXX();
$this->var1->... // The completion here doesn't work...

Posted by Ben on August 20, 2008 at 01:05 PM CEST #

Hi Ben,

this is a bug. It's already entered in our database. You can track it here: http://www.netbeans.org/issues/show_bug.cgi?id=144325

Posted by Petr on August 20, 2008 at 03:45 PM CEST #

So for me ^^"

Another question for fun.

Is there a method movement / typing column?
Like this :
http://manual.macromates.com/en/working_with_text#column_selections

Posted by Ben on August 21, 2008 at 01:26 PM CEST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed