X

An Oracle blog about NetBeans for PHP

Generating Getters and Setters Improved

Today I have committed to the trunk small improvement of generating getters and setters for class properties. It's based on your suggestion and as you can see on the pictures, you can select the way, how to generate the getter and setter method name.

There is a new combo box that contains two items which display the result of the method name for the first class field. On the picture you can see that you can select between two ways. The first one is the way which is used in NetBeans 6.5 and the second one is with underscore between get/set and property name.

There is the first question: Does somebody use different form of the getter and setter name? If yes, please speak up and I will update the dialog.

The enhancement is not fully finished. The value of the combo box now is not remembered, but I will add functionality, which will remember it per project. So the second question is: Do you use different form of getter and setter method names in different projects? Or you use just one way? Should be project or global options?

When I tested the enhancement then I have thinking to add also option for generating Php doc stub. But I haven't time to finish it yet.

Join the discussion

Comments ( 31 )
  • jozef Monday, November 10, 2008

    Look at Eclipse. It has very flexible configuration for getters/setters generator per project. I like it. I can use private properties with some prefix, e.g. private _name; or protected m_name. I configure Eclipse to identify those prefixes, so the getters/setters are always the same, setName() or set_name(), etc.


  • chad Monday, November 10, 2008

    I normally have variables in the form of myVar, and use getters and setters in the form of getVar and setVar.


  • fortruth Monday, November 10, 2008

    Hello, Petr.

    I want to know if there is a funciton that can select and complete a path when a input a file or folder path to the file. just like the code completion popup. Some times I always forget the real path name. If there is, it will do me good help to write my program


  • Petr Tuesday, November 11, 2008

    Hi Fortruth,

    I'm not sure if I understand correctly, but there is file system code completion for include and required functions. Also for finding a file you can use Navigation -> Go To File dialog. Could you describe the case, where you need it?

    Regards,

    Petr


  • Joaquin Fernandez Campo Wednesday, November 12, 2008

    Hi! Any news on the framework support?

    Congratulations on the great work you are doing!

    Thanks, Joaquín


  • Joaquin Thursday, November 13, 2008

    Hi Joaquin,

    I plan this weekend a little work on the Prado support and officially we are starting on Symfony support.

    Regards,

    Petr


  • Jaime M. Tan Tuesday, November 18, 2008

    Sorry for my bad English.

    I use setters & getters with magic methods __get __set is very easy to work with this functions...


  • Adam Jorgensen Thursday, November 20, 2008

    It would be nice if the IDE was, at some point, able to generate skeleton magic __get/__set methods for classes based on the define PHPDOc @property tags.

    A basic example of a __get method would be:

    public function __get($strName) {

    switch ($strName) {

    case 'strA': return $this->strA;

    case 'intB': return $this->intB;

    default:

    return parent::__get($strName);

    }

    }

    A basic example of __set would be:

    public function __set($strName, $mixValue) {

    switch ($strName) {

    case 'strA':

    $this->strA = $mixValue;

    return true;

    default:

    return parent::__set($strName, $mixValue);

    }

    }


  • Petr Thursday, November 20, 2008

    Hi Adam,

    good idea. Simple to implement. I will try to do it. Could you file enhancement in our Issuezilla? ( http://www.netbeans.org/community/issues.html , component PHP, subcomponent editor)?

    Thanks,

    Petr


  • Marat Fedorov Monday, December 8, 2008

    Hi

    For field "fileName" generate getter with name "getFilename" not "getFileName".


  • Blake Wednesday, December 10, 2008

    If I had:

    private $myVar;

    I would expect the name of the getter/setter to be:

    public getMyVar();

    public setMyVar();

    Currently it seems to only do getMyvar and setMyvar


  • Petr Wednesday, December 10, 2008

    To Marat, Blake: You are right. This is a bug. I will fix this. ( http://www.netbeans.org/issues/show_bug.cgi?id=155158 )

    Thanks,

    Petr


  • Michel Filipe Tuesday, December 16, 2008

    Hi! Very good this feature!

    My code standard is based in http://www.dagbladet.no/development/phpcodingstandard/.

    Example:

    SetName() and GetName()

    Bye!


  • Petr Tuesday, December 16, 2008

    Thanks to Michel for the link.


  • Krystian kuczek Monday, January 19, 2009

    Hi.

    it's very good but maybe u should use some template not select, for example set_%name% for set_something and for SetSomething Set%Name%, etc. Then everyone can choose option that is good for him. In this solution it's impossible.


  • Petr Monday, January 19, 2009

    Hi Krystian,

    you are right this is the best solution and it should be done in this way. On the other hand it cost some resources, which are not available now. So we will see and I hope that we will find more time to finish this feature in this way.


  • Ankara Bilgisayar Bilgisayar Satis Tuesday, February 3, 2009

    Thank for this nice article!


  • alexey_baranov Wednesday, February 4, 2009

    I prefere another style for protected variables. $_name for example. "_" at the beginning means "private" or "protected" for me.

    I would like to have getName() getter for $_name variable

    Thanks.


  • chat Sunday, March 8, 2009

    thanks very nice


  • kelebek Sunday, April 26, 2009

    thanks..


  • Dustin Graham Friday, May 29, 2009

    Please support $_variableName style variables for Getters and Setters.

    Web have been migrating our site from cold fusion to PHP over the past few months. Initially, we started using Eclipse PDT, but when we found that it doesn't support refactoring or generating of Getters and Setters and custom formatting, I decided to explore NetBeans which I had only previously used for Java. I have to say I am very very impressed with the full featured offerings I found. I've decided to make it my primary IDE for PHP rather than going for the Zend Studio, except I have one snag. when I generate getters and setters from class variables of the $_variableName style (which is our site-wide convention) I get methods like this: "get_variablename()" and my camel casing is removed and it leaves my underscore. Our standard getter and setter syntax is getVariableName().

    Would it be possible to add this to the drop down list of options? Thank you.

    Also, I do appreciate the added support for php code formatting options in the 6.7 beta release as far as the option to put the curly brace below the first line (also one of our conventions). Is there any more planned support for other formatting options such as those for for loops, if/else/switch statements. We have a lot of coding conventions enforced on our team and it would be great to have these types of options to speed up the work.

    Thank you very much.


  • patchinko Tuesday, July 28, 2009

    Hi, it would be great if next version includes support to $_variableName in the form of getVariableName() setVariableName().

    As many other people i use it to name protected and private members.

    Thx!


  • konya chat Thursday, August 20, 2009

    thanks admin


  • rodp Monday, May 24, 2010

    Hi, I agree. Now that 6.9 supports Zend Framework which uses underscore prefixes for private/protected class variables, supporting this would be extremely helpful

    Thanks


  • Ben Waine Wednesday, August 11, 2010

    Agreed, I'm crying out for $_variable Getter / Setter support!

    Thanks for the great prduct.

    Ben


  • norbi771 Monday, September 6, 2010

    Thank you, this is great functionality.

    I have a question however.

    I am trying to make Netbeans the only tool used in development and have development done as fast as possible. Based on database / table structure I would like to generate a class in PHP. Could you give me some guidance on how to tackle this problem? Is this alt-insert functionality hardcoded into netbeans or is this kind of module I could customize/extend somehow?

    Thank you for your answer.

    Best regards

    Norbert


  • Petr Pisl Wednesday, September 8, 2010

    To Norbert:

    the alt-inert functionality is really hartcoded, but you can write your own plugin that will add new item there and generate what you want. What exactly do you want?

    Regards,

    Petr


  • norbi771 Thursday, September 9, 2010

    To Petr Pisl:

    Thank you for your answer. I am very new to java and although I fully understand the semantics and can read the code without problems with understanding it, I have no experience in Java coding at all. Tried a bit but no hurahh ... yet. I thought that maybe getter/setter and Database Table generators were based on some kind of template I could simply adjust but you confirmed they were not.

    However, in netbeans sources I found code responsible for generating getter/setter and Database table. These modules are very good examples for my case.

    Following your advice I will try write netbeans module doing what I want, thank you.

    Brgs

    Norbert


  • Petr Pisl Thursday, September 9, 2010

    To Norbert: Some times you need some information from source model. Like in getter / setter example we check, whether there is not a method with the same name. This is reason why you need to do it in java. In your case, you will need information from database model.

    Regards,

    Petr


  • guest Wednesday, May 2, 2012

    I'm using this great feature all the time - but i always have to correct each generated function by hand, as my classes normally implement the fluent interface (which means all setters end returning $this) and my coding standards require a newline b4 an opening brace.

    Is there any way for me of editing the "template" that is used for function generation? Anyway i think adding e.g. an option checkbox to generate fluid interface setters would be an awesome feature - i simply cannot be the only programmer out there preferring his/her setter fluid ;)


  • Ondrej Brejla Wednesday, May 9, 2012

    Hi, there is an enhancement in our issuezilla for this feature. I would like to look at it for next release.


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