X

An Oracle blog about NetBeans for PHP

Generate Constructor, Getters and Setters

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.

Join the discussion

Comments ( 29 )
  • Azrael Valedhel Monday, July 21, 2008

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

    Keep it up!


  • Alexius Monday, July 21, 2008

    Super!!! Like in Java. Thanks!!


  • Guido Monday, July 21, 2008

    OH YES, i am waiting so long for this feature.

    when goes it online ?

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


  • Petr Tuesday, July 22, 2008

    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


  • Courtney Miles Tuesday, July 22, 2008

    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.


  • Petr Wednesday, July 23, 2008

    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


  • Rus Wednesday, July 23, 2008

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


  • Petr Wednesday, July 23, 2008

    On Mac the short cut is CTRL+I.

    Regards,

    Petr


  • Sebastian Friday, August 1, 2008

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


  • Petr Friday, August 1, 2008

    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


  • Sebastian Monday, August 11, 2008

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

    Thanks,

    Seb.


  • Kevin Bond Tuesday, August 12, 2008

    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


  • Petr Wednesday, August 13, 2008

    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


  • Ben Wednesday, August 20, 2008

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


  • Petr Wednesday, August 20, 2008

    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


  • Ben Thursday, August 21, 2008

    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


  • Alexandre Simon Tuesday, September 15, 2009

    Great feature! I am currently a user of PDT / Zend Studio for Eclipse and this feature is only available in the last zend studio version.

    Is it possible to generate the phpdoc with it ?

    I mean if I wrote the class A :

    class A

    {

    /\*\*

    \* @var String

    \*/

    private $aString;

    }

    is it possible to obtain the following ?

    class A

    {

    /\*\*

    \* @var String

    \*/

    private $aString;

    /\*\*

    \* @return String

    \*/

    public function getAString() ... ;

    /\*\*

    \* @param String $aString

    \*/

    public function setAString($aString) ... ;

    }


  • Andreas Rein Tuesday, September 22, 2009

    Hi,

    sorry, this is my article written in german.

    Seit kurzem versuche ich meine PHP-Scripte mit NetBeans IDE 6.7.1 (Build 200907230101) zu schreiben. Bisher bin ich auch sehr zu frieden.

    Ich halte mich bei der Codegenerierung an "Zend Framework Coding Standard für PHP"

    Dort wird empfohlen, siehe (http://framework.zend.com/manual/de/coding-standard.naming-conventions.html#coding-standard.naming-conventions.variables)

    … Für Instanzvariablen die mit dem "private" oder "protected" Modifikator deklariert werden, muß das erste Zeichen des Funktionsnamens ein einzelner Unterstrich sein. …

    <code>

    <?php

    class Test

    {

    protected $_var; // or private

    public function get_var() {

    return $this->_var;

    }

    public function set_var($_var) {

    $this->_var = $_var;

    }

    }

    // -> so, wäre es richtig:

    class Test

    {

    protected $_var;

    public function getVar() {

    return $this->_var;

    }

    public function setVar($var) {

    $this->_var = $var;

    }

    }

    </code>

    Es wäre schön, wenn Sie das in einer neueren Version berücksichtigen könnten, DANKE.

    Viele Grüße aus Deutschland – Warburg

    Andreas Rein


  • Erg&uuml;n KO&Ccedil;AK Saturday, November 21, 2009

    Hello,

    Great work :) and i have a question :)

    is it possible to change getter setter templates ?

    Best Regards,


  • dazz Monday, December 7, 2009

    Hi,

    I, too, want to change the templates for getters and setter to adapt to my coding guideline.

    Is there a way to change it?

    Kinde regards,


  • Petr Pisl Monday, December 7, 2009

    Hi Dazz,

    unfortunately there is no way how to customize the getter and setter. I want to do it during NetBeans 6.9 release.


  • Agus Suhartono Thursday, June 17, 2010

    On Netbeans 6.9, this feature still not work for Zend Framework naming convention (http://framework.zend.com/manual/en/coding-standard.naming-conventions.html#coding-standard.naming-conventions.variables)

    this is sample from Alexandre Simon :

    class Test

    {

    protected $_var; // or private

    public function get_var() {

    return $this->_var;

    }

    public function set_var($_var) {

    $this->_var = $_var;

    }

    }

    // -> so, wäre es richtig:

    class Test

    {

    protected $_var;

    public function getVar() {

    return $this->_var;

    }

    public function setVar($var) {

    $this->_var = $var;

    }

    }


  • ale Thursday, February 10, 2011

    thanks a lot!!!!!!!!!!!


  • karel barel Thursday, March 24, 2011

    Hello Petr,

    is it possible to customize getter / setter templates in Netbeans 7.0 beta or in nightly builds and if it is then how please?

    Thanks


  • umpirsky Tuesday, April 19, 2011

    Really, really gret. I like this feature very much.

    It would be nice to generate appropriate PHPDoc as well. For example:

    /\*\*

    \* Set name.

    \*

    \* @param string $name

    \*/

    public function setName($name) {

    $this->name = $name;

    }


  • umpirsky Tuesday, April 19, 2011

    Really, really gret. I like this feature very much.

    It would be nice to generate appropriate PHPDoc as well.


  • Steve Lawson Sunday, February 19, 2012

    In the NetBeans Help document for Code Generators there's a broken page access point: Clicking "improved getter and setter generation" generates a "404 Not Found!" error.


  • guest Friday, March 9, 2012

    How to modify NetBeans settings to change getters/setters generated naming convention?

    I wanna have "void MyValue(argType arg)" as setter and "argType MyValue()" as getter.


  • kovosz Monday, August 26, 2013

    Many thanks for this feature, you saved me a lot of time!


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