Working with Refactoring

The purpose of this tutorial is to teach you how to refactor your code to easily rename and move files and elements without damaging your projects or severing the link between referenced items, as well as creating the necessary links between PHP elements in separate files.

Purpose and Usage

The Refactoring feature allows you to:

  • Rename and move files and elements within those files, while maintaining the links between the items. Once an element or file has been renamed or moved, all instances of that item within the project will be automatically updated to reflect its new name / location.

  • Organize Includes so that elements from one file can be referenced in another file.

Refactoring should be used when you are reorganizing your project and changing names and locations of files and elements.

Note:

Refactoring will only work from within PHP Explorer view and not from Navigator view.

Back to Top

Renaming / Moving Files

Renaming a file

Renaming a file will result in the automatic renaming of all instances where that file is referenced within the project.

 

 

Instructions on how to complete a procedure

This procedure demonstrates how to Rename a file:

  1. Create a PHP file, called RenFile1, with the following code:

<?
$a = 5;
?>

  1. In the same project, create a second PHP file, called RenFile2, with the following code:

<? require(RenFile1.php);$a = 8;?

Note:

Ensure you delete the PHP tags that are inserted by default when creating a new PHP file before pasting in the code.

  1. Save both files.

  2. In PHP Explorer view, right-click RenFile1 and select Refactor | Rename – or- select it and go to Refactor | Rename from the Main Menu.
    A Rename File dialog will appear.

  3. In the Rename File dialog box, rename RenFile1 to RenFile3.

  4. Check the "Update references" box and click Preview.

  5. In the Preview window, scroll through the changes and note that, as well as the name of the file itself being changed, the reference to the file in RenFile2 will also have been changed.

  6. Press OK to apply changes.

The reference to RenFile1 in RenFile2 will have been updated to:

require(RenFile3.php);

in order to reflect the changes in the file name.

Moving a file

Moving a file will result in the automatic updating of all instances where that file is referenced within the project to reflect its change of location.

 

 

Instructions on how to complete a procedure

This procedure demonstrates how to move a file:

  1. Create RenFile1 and RenFile2 as described in steps 1 to 3 underRenaming a File, above.

  2. Within the same project, create an additional folder called RenFolder.

  3. In PHP Explorer view, right-click RenFile1 and select Refactor | Move -or- select it and go to Refactor | Move from the Main Menu.

  1. In the Move dialog, select RenFolder.

  2. Click Preview.

The Preview windows will display the changes that the move will apply to your script. Note that RenFile1's new location will automatically be updated in the reference to it in RenFile2.

  1. Click OK to execute the Move.

 

Back to Top

Renaming Elements within a File

All PHP Elements (e.g. classes, interfaces, functions, methods) can also be renamed and refactored from within the editor window.

 

 

Instructions on how to complete a procedure

This procedure demonstrates how to use the Rename Elements feature:

  1. Create 2 files (RenFile1 and RenFile2) as described in steps 1 to 3 under "Renaming a File", above.

  2. In the editor window of RenFile2, highlight the variable "a" on line 3.

  3. Right-click and select Refactor | Rename -or- click Alt+Shift+R.

  4. In the Rename Global Variable dialog box, rename the variable to b.

  5. Check the "Update textual occurrences" box and click Preview.

  6. In the Preview window, scroll through the changes and note that occurrences of variable "$a" will be changed in RenFile1 as well as in RenFile2.

  7. Press OK to apply changes.

 

Back to Top

Organizing Includes - (Studio6)

Using the Organizing Includes feature will allow PHP objects created in one file to be called into other files within the project.

 

 

Instructions on how to complete a procedure

This procedure demonstrates using the Organizing Includes feature:

  1. Create a new PHP file called OIFile1 with the following code:

<?php

class MyClass {}

?>

  1. Create a second PHP file, called OIFile2, with the following code:

<?php

function myFunction() {

return 1;

}

?>

  1. Create a third PHP file, called OIFile3, with the following code:

<?php

$z = new MyClass();

echo myFunction();

?>

Note that the code in OIFile3 attempts to call MyClass and myFunction created in OIFiles 1 and 2.

  1. Save the files.

  2. Right-click within OIFile3 and select Refactor | Organize Includes – or – go to Refactor Menu and select Organize Includes.

  3. In the Organize Includes dialog, a display will show that instances of MyClass and myFunction have been found in the previous files.

  4. Scroll down to preview the changes. Note that that include_once will now be added for OIFile1 and OIFile2 so that MyClass and myFunction can be called.

  5. Click OK to apply the changes.

Note:

If two elements of the same name are recognized, a window will display so that the relevant one can be selected.

 

The Organize Includes feature can also:

  • Delete references and "include" calls to files if the relevant items have been deleted from the code.

  • Move "include" calls to the top of your script if they have been placed further down, in order for the elements to be referenced before the script is run.

Back to Top

 

 

Related Links:

Refactoring

Using Refactoring

Renaming Files

Renaming Elements

Organizing Includes  

Refactor Menu

 

 

Back to Top