PhpStorm 7.1.0 Web Help

Refactor | Move
F6F6F6F6F6F6Ctrl+MShift+Alt+VF6F6Alt Command V


The dialog box opens when you position the cursor at the name of the class in the editor and invoke the refactoring. In this dialog box:

  1. Appoint the namespace to move the selected class to. This can be:
    • A new namespace under the current namespace.
    • Another subordinate namespace under the current parent.
    • An existing or a new namespace under another parent.
    In any case, the import statements referencing the class are updated accordingly.
  2. Appoint the folder where you want PhpStorm to move the file that implements the class. PhpStorm supposes that the namespaces in your project are arranged in compliance with the PSR0 standard and enforces you to retain the folder structure and the namespace hierarchy in accordance when moving classes among namespaces. When you specify a namespace to move a class to, PhpStorm automatically updates the path to the folder where the file that implements the class in question will be stored. By default, this path is read-only. You can edit it by pressing F2 keeping in mind that this may cause problems with automatic loading in the future.
Item Description
Move class to namespace When the dialog box opens, this filed shows the namespace to which the class belongs. Specify the fully qualified name of the target namespace. Depending on your use case, this can be:
  • A new namespace under the current namespace in the format <current namespace>\<new namespace>.
  • Another subordinate namespace under the current parent in the format <path to immediate parent namespace>\<namespace to move the class to>.
  • An existing or a new namespace under another parent namespace in the format <path to another root namespace>\<namespace to move the class to>.
Use only backslashes (\) as namespace separators.
Move class to a new file
  • Select this check box to have the file that implements the class moved to another directory. This ensures that in your project the folder structure corresponds with the namespace hierarchy in accordance with the PSR0 standard.
  • Clear this check box to have the class moved to another namespace without moving the file that implements the class to the corresponding directory.
Target destination directory When the dialog box opens, the field shows the path to the folder where the file that implements the class to move is currently stored. The path is displayed in the format ...\<project root folder>\<path to the current namespace folder relative to the project root>. The path is read-only and is updated automatically as you specify the namespace to move the class to. You can edit it by pressing F2 keeping in mind that this may cause problems with automatic loading in the future.

Examples of moving classes

Moving a class to a new namespace under the current namespace

Suppose, your project move_class_refactoring has a namespace namespace1 with one class Class1 implemented in the file Class1.php.

ps_move_class_1.png

Let's create a subordinate namespace namespace1\ns1 and move Class1 to it.

  1. Open Class1.php in the editor, position the cursor at Class1, and press F6F6F6F6F6F6Ctrl+MShift+Alt+VF6F6Alt Command V. The Move Class dialog box opens.

    ps_move_class_2

  2. The Move class Class1 to namespace field shows the current namespace namespace1. Change it to namespace1\ns1.
  3. As you type, the Target destination directory read-only field is updated automatically from ...\move_class_refactoring\namespace1 to ...\move_class_refactoring\namespace1\ns1.

    ps_move_class_3

  4. Press Refactor. As a result, PhpStorm creates a new subfolder ns1 and moves the file Class1 to it. Accordingly, the class Class1 is moved to the namespace1\ns1 namespace:

    ps_move_class_4

Moving a class between two namespaces under the same parent

Suppose, your project move_class_refactoring has a namespace namespace1 with two subordinate namespaces namespace1\ns1 and namespace1\ns3. These subordinate namespaces have Class1 Class3 respectively. In accordance with the PSR0standard, these classes are implemented in the files Class1.php and Class3.php that are stored in the folders ...\move_class_refactoring\namespace1\ns1 and ...\move_class_refactoring\namespace1\ns3 respectively.

ps_move_class_5.png

Let's move Class3 from namespace1\ns3 to namespace1\ns1.

  1. Open Class3.php in the editor, position the cursor at Class3, and press F6F6F6F6F6F6Ctrl+MShift+Alt+VF6F6Alt Command V. The Move Class dialog box opens.

    ps_move_class_6

  2. The Move class Class3 to namespace field shows the current namespace namespace1\ns3. Change it to namespace1\ns1.
  3. As you type, the Target destination directory read-only field is updated automatically from ...\move_class_refactoring\namespace1\ns3 to ...\move_class_refactoring\namespace1\ns1.

    ps_move_class_7

  4. Press Refactor. As a result, PhpStorm moves the class Class3 from namespace1\ns3 to namespace1\ns1. Accordingly, the file Class3 is moved from folder ...\move_class_refactoring\namespace1\ns3 to folder ...\move_class_refactoring\namespace1\ns1:

    ps_move_class_8

Moving a class to a namespace under another parent namespace

Suppose, your project move_class_refactoring has two parent namespaces namespace1 and namespace2 with subordinate namespaces namespace1\ns1 and namespace2\ns2. Class2 is defined in namespace2\ns2, and in accordance with the PSR0standard the file Class2.php that implements it is stored in ...\move_class_refactoring\namespace2\ns2.

ps_move_class_9.png

Let's move Class2 from namespace2\ns2 to namespace1\ns1.

  1. Open Class2.php in the editor, position the cursor at Class2, and press F6F6F6F6F6F6Ctrl+MShift+Alt+VF6F6Alt Command V. The Move Class dialog box opens.

    ps_move_class_10

  2. The Move class Class2 to namespace field shows the current namespace namespace2\ns2. Change it to namespace1\ns1.
  3. As you type, the Target destination directory read-only field is updated automatically from ...\move_class_refactoring\namespace2\ns2 to ...\move_class_refactoring\namespace1\ns1.

    ps_move_class_11

  4. Press Refactor. As a result, PhpStorm moves the class Class2 from namespace2\ns2 to namespace1\ns1. Accordingly, the file Class2 is moved from folder ...\move_class_refactoring\namespace2\ns2 to folder ...\move_class_refactoring\namespace1\ns1:

    ps_move_class_12

See Also

Procedures:

Web Resources: