Rename Refactoring

Rename is one of the most useful refactorings of all. The developer can alter the name of a selected type, type member, function, or constant in the code. Users of NetBeans PHP IDE have been able to use the Instant Rename feature since 6.5. Instant Rename renames all elements in the scope of one file. Now in daily builds the user can also use Rename Refactoring. Rename Refactoring can rename elements not only in the scope of one file but also look up all appropriate occurrences in the whole project and then rename them.  Simply put the caret on the identifier and press Ctrl+R. Local changes are still made instantly in-place by Instant Rename, which is true e.g. for local variables, parameters in functions or methods and the same for private type members. 


So pressing Ctrl+R for $mainModule variable invokes in-place editing, but not the same if e.g. class name should be renamed: 


Although the same shortcut Ctrl+R was used, the behavior and UI is different for renaming the class name as shown on the picture above - comparing to rename of parameter. After pressing "Preview" button the individual changes will be listed like on the next picture:


Purpose of checkboxes is probably obvious - include/exclude appropriate changes. By going through the list the individual diffs are shown:


Use "Do refactoring" to apply changes. To revert the changes Refactoring > Undo should help.

Please report any issues or request any enhancements in NetBeans Bugzilla


I'm using NB 6.9 Build 201007282301 and there is a big difference between using ctrl-R and choosing Refactor > Rename on the context menu or the main menu.

If I use Refactor > Rename I get a message saying "The Rename refactoring cannot be applied in this context", while using ctrl-R works as it's meant to. I only found that out by reading this post, as I hadn't tried ctrl-R as I figured it was the same behaviour as using the menu.

Not sure if this is a bug or feature...


Posted by Cameron Junge on August 24, 2010 at 04:34 PM CEST #

To Cameron Junge:
Did you use the Night Build from here:
This one works either from main menu or context menu.

I am using PHP's class auto-load feature in my projects, where name of class must have same name as php file. In Netbeans and Java this refactoring cover also file name. Can be this done in PHP as well?

Posted by Raazy on August 25, 2010 at 02:37 AM CEST #

To Cameron Junge:
I think its bug in 6.8 everything was ok ctr+r works as same as refactor from menu now you have to use ctr+r not refactor from menu when you want to change variable name in one class in one function

Posted by Kamil on August 25, 2010 at 03:43 AM CEST #

With the acutal 6.9.1, I've got the same problem: Refactoring only works with <Ctrl>+<R> but never from the context or the normal menue "Refactor".

Product Version: NetBeans IDE 6.9.1 (Build 201007282301)
Java: 1.6.0_21; Java HotSpot(TM) 64-Bit Server VM 17.0-b17
System: Windows 7 version 6.1 running on amd64; Cp1252; de_DE (nb)

Downloaded from:

Posted by Mike on August 25, 2010 at 04:20 AM CEST #

Posted by Mike on August 25, 2010 at 04:24 AM CEST #

Two features:
a/ Rename Refactoring (Refactor > Rename in menu)
b/ Instant Rename (Ctrl+R)

In 6.9, 6.9.1 just b/ was implemented => just Ctrl+R works

In daily builds both a/ and b/ implemented => Ctrl+R works and Refactor > Rename also.

Ctrl+R in daily builds invokes Instant Rename(e.g. for parameters) or Rename Refactoring(e.g. for class names)

Posted by radek on August 25, 2010 at 04:25 PM CEST #

To Raazy:
name of class doesn't need to have the same name as file in php, so coupling file and class name when renamed file makes sense just for special usecases

On the other hand include, require statements referencing renamed file name - should be probably changed

Posted by radek on August 25, 2010 at 05:04 PM CEST #

To radek:
True, it does not have to. Such special cases are when project uses autoload feature. Yes, there exists many exceptions such as prefixed or suffixed filename, multiple classes in one file etc. where is impossible to predict right filename during refactoring.
I am using Netbeans for J2ME applications also and there refactoring goes as deep as I described.

So I think if filename.php == classname, renaming of that file CAN be offered as well. To prevent mess, this checkbox will be unchecked by default.


Posted by Raazy on August 26, 2010 at 02:35 AM CEST #


I just tried the preview and it seems to work great. I am very excited about this feature! :)

I do have a small proposal/question though. What about refactoring strings i cases like factories? e.g. renaming FooClass when there are use cases like this:


Maybe based on a comment annotation e.g.

/\*\* @param Class $class \*/
public function factory($class);

Posted by Martin Vium on August 26, 2010 at 03:01 AM CEST #

On second thought. It might not be plausible, in regards to class prefixing and namespaces, which are common here.

Posted by Martin Vium on August 26, 2010 at 03:05 AM CEST #

To Martin Vium:
plausibility cannot be achieved here, but we can improve it still a lot for individual use cases - one of them is the one that you mention - so please report it (not to forget about it):

To Raazy:
yes, the checkbox in case that the file contains class with the same name makes sense. So, please report it also

Posted by radek on August 26, 2010 at 03:21 AM CEST #

To radek:

Thanks much. I filed it here:

Posted by Raazy on August 26, 2010 at 06:35 AM CEST #

i try it in my nb 6.9.1(simple chinese edition),but it dosen't work in any case.

Posted by chen on October 10, 2010 at 07:30 AM CEST #

i try it in my nb 6.9.1(simple chinese edition),but it dosen't work in any case.

Posted by chen on October 10, 2010 at 07:38 AM CEST #

The feature is in the nightly build of what will (probably) be 7.0, Not the 6.9.1 release from July.

Posted by Jeffrey Rubinoff on October 19, 2010 at 08:28 AM CEST #

I'm using 7 Beta. Is there a way to force an instant rename on a variable? Sometimes I know that I only need to change it in the one file.

Posted by Charles on November 24, 2010 at 03:06 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed

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


« July 2016