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.

Comments:

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

Posted by Azrael Valedhel on July 21, 2008 at 06:28 PM CEST #

Super!!! Like in Java. Thanks!!

Posted by Alexius on July 21, 2008 at 10:08 PM 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 01: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 02: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 22, 2008 at 06:52 PM 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 04: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 07:54 AM CEST #

On Mac the short cut is CTRL+I.

Regards,
Petr

Posted by Petr on July 23, 2008 at 08:05 AM CEST #

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

Posted by Sebastian on August 01, 2008 at 03: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 08:57 AM 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 06:40 AM 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 12: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 05:36 AM 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 06:05 AM 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 08:45 AM 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 06:26 AM CEST #

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

Posted by Alexandre Simon on September 15, 2009 at 11:46 AM CEST #

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

Posted by Andreas Rein on September 22, 2009 at 03:21 AM CEST #

Hello,

Great work :) and i have a question :)
is it possible to change getter setter templates ?

Best Regards,

Posted by Ergün KOÇAK on November 21, 2009 at 03:46 PM CET #

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,

Posted by dazz on December 07, 2009 at 03:28 AM CET #

Hi Dazz,

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

Posted by Petr Pisl on December 07, 2009 at 03:53 AM CET #

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;
}
}

Posted by Agus Suhartono on June 17, 2010 at 03:13 AM CEST #

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

Posted by ale on February 10, 2011 at 05:13 PM CET #

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

Posted by karel barel on March 24, 2011 at 01:41 AM CET #

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;
}

Posted by umpirsky on April 19, 2011 at 07:28 AM CEST #

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

It would be nice to generate appropriate PHPDoc as well.

Posted by umpirsky on April 19, 2011 at 07:30 AM CEST #

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.

Posted by Steve Lawson on February 19, 2012 at 10:34 PM CET #

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.

Posted by guest on March 09, 2012 at 06:19 PM CET #

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

Posted by kovosz on August 26, 2013 at 02:40 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

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

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today