An Oracle blog about NetBeans for PHP

Rename Refactoring

Guest Author

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

Join the discussion

Comments ( 16 )
  • Cameron Junge Tuesday, August 24, 2010

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


  • Raazy Wednesday, August 25, 2010

    To Cameron Junge:

    Did you use the Night Build from here: http://bertram.netbeans.org/hudson/job/PHP-build/

    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?


  • Kamil Wednesday, August 25, 2010

    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

  • Mike Wednesday, August 25, 2010

    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: http://netbeans.org/downloads/index.html

  • Mike Wednesday, August 25, 2010
  • radek Wednesday, August 25, 2010

    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)

  • radek Wednesday, August 25, 2010

    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

  • Raazy Thursday, August 26, 2010

    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.


  • Martin Vium Thursday, August 26, 2010


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

  • Martin Vium Thursday, August 26, 2010

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

  • radek Thursday, August 26, 2010

    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): http://netbeans.org/community/issues.html

    To Raazy:

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

  • Raazy Thursday, August 26, 2010

    To radek:

    Thanks much. I filed it here: https://netbeans.org/bugzilla/show_bug.cgi?id=189820

  • chen Sunday, October 10, 2010

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

  • chen Sunday, October 10, 2010

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

  • Jeffrey Rubinoff Tuesday, October 19, 2010


    The feature is in the nightly build of what will (probably) be 7.0, http://bertram.netbeans.org/hudson/job/PHP-build/. Not the 6.9.1 release from July.

  • Charles Wednesday, November 24, 2010

    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.

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