Tutorial: Moving PHP classes
In this tutorial, we will explore how to use Move Refactorings for moving PHP classes between namespaces in a project.
Moving a class to a new namespace under the current namespace
Suppose, your move_class
project has the namespace1
namespace, with the Class1
class implemented in the Class1.php file.
Let's create a subordinate namespace namespace1\ns1
and move Class1
to it.
Open Class1.php in the editor, position the caret at
Class1
, and press F6. The Move Class dialog opens.The Move class Class1 to namespace field shows the current namespace
namespace1
. Change it tonamespace1\ns1
.As you type, the Target destination directory field is updated automatically from
...\move_class\namespace1
to...\move_class\namespace1\ns1
.Press Refactor. As a result, PhpStorm creates the ns1 subfolder and moves Class1.php to it. Accordingly, the
Class1
class is moved tonamespace1\ns1
:
Moving a class between two namespaces under the same parent
Suppose, your move_class
project has the namespace1
namespace with two subordinate namespaces: namespace1\ns1
and namespace1\ns3
. These subordinate namespaces have Class1
and Class3
respectively. In accordance with the PSR0 standard, these classes are implemented in the Class1.php and Class3.php files that are stored under ...\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 caret at
Class3
, and press F6. The Move Class dialog 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 field is updated automatically from
...\move_class\namespace1\ns3
to...\move_class\namespace1\ns1
.Press Refactor. As a result, PhpStorm moves the
Class3
class fromnamespace1\ns3
tonamespace1\ns1
. Accordingly, the Class3.php file is moved from the ...\move_class\namespace1\ns3 folder to the ...\move_class\namespace1\ns1 folder:
Moving a class to an existing namespace under another parent namespace
Suppose, your move_class
project has two parent namespaces, namespace1
and namespace2
, with subordinate namespaces namespace1\ns1
and namespace2\ns2
. The Class2
class is defined in namespace2\ns2
, and in accordance with the PSR0 standard, the Class2.php file 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 caret at
Class2
, and press F6. The Move Class dialog 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 field changes automatically from
...\move_class\namespace2\ns2
to...\move_class\namespace1\ns1
.Press Refactor. As a result, PhpStorm moves the
Class2
class fromnamespace2\ns2
tonamespace1\ns1
. Accordingly, Class2.php is moved from ...\move_class\namespace2\ns2 to ...\move_class\namespace1\ns1:
Moving a class to a new namespace outside the immediate parent namespace
Suppose, your move_class
project 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 Class2.php file 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 ...\move_class\namespace3 folder for namespace3
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 caret at
Class2
, and press F6. The Move Class dialog opens.The Move class Class2 to namespace field shows the current
namespace2\ns2
namespace. 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 determine the path to the folder for the new namespace
namespace3
to be created:To solve the problem, press F2 and type the path manually:
...\move_class\namespace2\ns2Alternatively, specify the root folder for your namespaces by marking the ...\move_class\ folder as Sources as described in Marking directories. Note that this should be done before you start the refactoring, so if you have already opened the Move Class dialog, close it first.
After you have appointed ...\move_class\ as the root for your namespaces, PhpStorm will determine the folder for the new namespace and update the Target Destination Directory field automatically:
Press Refactor. As a result, PhpStorm moves
Class2
fromnamespace2\ns2
tonamespace3
. Accordingly, Class2.php is moved from ...\move_class\namespace2\ns2 to ...\move_class\namespace3: