Move Class Dialog
The dialog box opens when you position the cursor at the name of the class in the editor and invoke the refactoring.
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 with this standard when moving classes among namespaces.
When you specify a namespace to move a class to, PhpStorm automatically updates the Target Destination Directory field, which shows the path to the folder where the file that implements the class in question will be stored.
Item | Description |
---|---|
Move class <class name> to namespace | When the dialog box opens, this field 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:
\ ) as namespace separators. |
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 See Directories for details. Do one of the following:
|
Search in comments and strings | If this check box is selected, PhpStorm will look for references to the class to be moved within comments and string literals in source code files. |
Search for text occurrences | If this check box is selected, PhpStorm will look for references to the class to be moved within documentation, HTML, JSP, various configuration files (for example .yml ), and other files included in your project. |
Examples of Moving PHP Classes
Moving a Class to a New Namespace under the Current Namespace
Suppose, your project move_class
has a namespace namespace1
with one class Class1
implemented in the file Class1.php
. Let's create a subordinate namespace namespace1\ns1
and move Class1
to it.
- Open
Class1.php
in the editor, position the cursor atClass1
, and press F6. The Move Class dialog box opens. - The Move class Class1 to namespace field shows the current namespace
namespace1
. Change it tonamespace1\ns1
. - As you type, the Target destination directory read-only field is updated automatically from
...\move_class\namespace1
to...\move_class\namespace1\ns1
. - Press Refactor. As a result, PhpStorm creates a new subfolder
ns1
and moves the fileClass1
to it. Accordingly, the classClass1
is moved to thenamespace1\ns1
namespace:
Moving a Class between Two Namespaces under the Same Parent
Suppose, your project move_class
has a namespace namespace1
with two subordinate namespaces namespace1\ns1
and namespace1\ns3
. These subordinate namespaces have Class1
Class3
respectively. In accordance with the PSR0 standard, these classes are implemented in the files Class1.php
and Class3.php
that are stored in the folders ...\move_class\namespace1\ns1
and ...\move_class\namespace1\ns3
respectively. Let's move Class3
from namespace1\ns3
to namespace1\ns1
.
- Open
Class3.php
in the editor, position the cursor atClass3
, and press F6. The Move Class dialog box opens. - The Move class Class3 to namespace field shows the current namespace
namespace1\ns3
. Change it tonamespace1\ns1
. - As you type, the Target destination directory read-only field is updated automatically from
...\move_class\namespace1\ns3
to...\move_class\namespace1\ns1
. - Press Refactor. As a result, PhpStorm moves the class
Class3
fromnamespace1\ns3
tonamespace1\ns1
. Accordingly, the fileClass3
is moved from folder...\move_class\namespace1\ns3
to folder...\move_class\namespace1\ns1
:
Moving a Class to an Existing Namespace under Another Parent Namespace
Suppose, your project move_class
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 PSR0 standard the file Class2.php
that implements it is stored in ...\move_class\namespace2\ns2
. Let's move Class2
from namespace2\ns2
to namespace1\ns1
.
- Open
Class2.php
in the editor, position the cursor atClass2
, and press F6. The Move Class dialog box opens. - The Move class Class2 to namespace field shows the current namespace
namespace2\ns2
. Change it tonamespace1\ns1
. - As you type, the Target destination directory read-only field is updated automatically from
...\move_class\namespace2\ns2
to...\move_class\namespace1\ns1
. - Press Refactor. As a result, PhpStorm moves the class
Class2
fromnamespace2\ns2
tonamespace1\ns1
. Accordingly, the fileClass2
is moved from folder...\move_class\namespace2\ns2
to folder...\move_class\namespace1\ns1
:
Moving a Class to a New Namespace Outside the Immediate Parent Namespace
Suppose, your project move_class
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 PSR0 standard the file Class2.php
that implements it is stored in ...\move_class\namespace2\ns2
. Let's move Class2
from namespace2\ns2
to a new namespace namespace3
that does not exist yet. To meet the PSR0 requirements, a new folder ...\move_class\namespace3
for the namespace3
namespace should be also created.
When starting the refactoring we should keep in mind that the new namespace is to be created outside the immediate parent namespace of our class's namespace, therefore PhpStorm will not suggest a proper folder for it. So we can either specify the target folder manually or appoint a root folder for namespaces which will be the starting point for PhpStorm to calculate the folder for the new namespace.
- Open
Class2.php
in the editor, position the cursor atClass2
, and press F6. The Move Class dialog box opens. - The Move class Class2 to namespace field shows the current namespace
namespace2\ns2
. Change it tonamespace3
. The result depends on whether you have appointed the root folder for your namespaces or not.- If no root folder is specified, the Target destination directory field still shows the path to
...\move_class\namespace2\ns2
because PhpStorm cannot calculate the path to the folder for the new namespacenamespace3
to be created:To solve the problem, press F2 and type the path manually:
...\move_class\namespace2\ns2
- Alternatively, specify the root folder for your namespaces by marking the
...\move_class\
folder as Sources as described in Configuring Folders Within a Content Root. Note that this should be done before you start the refactoring, so if you have already opened the Move Class dialog box, close it first.After you have appointed the
...\move_class\
folder as the root for your namespaces, PhpStorm will calculate the folder for the new namespace and update the Target Destination Directory field automatically: - Press Refactor. As a result, PhpStorm moves the class
Class2
fromnamespace2\ns2
tonamespace3
. Accordingly, the fileClass2
is moved from folder...\move_class\namespace2\ns2
to folder...\move_class\namespace3
:
- If no root folder is specified, the Target destination directory field still shows the path to