PhpStorm 2022.3 Help

Tutorial: Moving PHP classes

In this tutorial, we will explore how to use Copy and 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.

Move Class project overview

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

  1. Open Class1.php in the editor, position the caret at Class1, and press F6. The Move Class dialog opens.

    the Move Class dialog
  2. The Move class Class1 to namespace field shows the current namespace namespace1. Change it to namespace1\ns1.

    As you type, the Target destination directory field is updated automatically from ...\move_class\namespace1 to ...\move_class\namespace1\ns1.

    the Move Class dialog
  3. Click Refactor. As a result, PhpStorm creates the ns1 subfolder and moves Class1.php to it. Accordingly, the Class1 class is moved to namespace1\ns1:

    Class1 moved

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.

Move Class project overview

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

  1. Open Class3.php in the editor, position the caret at Class3, and press F6. The Move Class dialog opens.

    the Move Class dialog
  2. The Move class Class3 to namespace field shows the current namespace namespace1\ns3. Change it to namespace1\ns1.

    As you type, the Target destination directory field is updated automatically from ...\move_class\namespace1\ns3 to ...\move_class\namespace1\ns1.

    the Move Class dialog
  3. Click Refactor. As a result, PhpStorm moves the Class3 class from namespace1\ns3 to namespace1\ns1. Accordingly, the Class3.php file is moved from the ...\move_class\namespace1\ns3 folder to the ...\move_class\namespace1\ns1 folder:

    Class 3 moved

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.

Move Class project overview

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

  1. Open Class2.php in the editor, position the caret at Class2, and press F6. The Move Class dialog opens.

    the Move Class dialog
  2. The Move class Class2 to namespace field shows the current namespace namespace2\ns2. Change it to namespace1\ns1.

    As you type, the Target destination directory field changes automatically from ...\move_class\namespace2\ns2 to ...\move_class\namespace1\ns1.

    the Move Class dialog
  3. Click Refactor. As a result, PhpStorm moves the Class2 class from namespace2\ns2 to namespace1\ns1. Accordingly, Class2.php is moved from ...\move_class\namespace2\ns2 to ...\move_class\namespace1\ns1:

    Class2 moved

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.

ps_move_class_9.png

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.

  1. Open Class2.php in the editor, position the caret at Class2, and press F6. The Move Class dialog opens.

    the Move Class dialog
  2. The Move class Class2 to namespace field shows the current namespace2\ns2 namespace. Change it to namespace3. 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:

      the Move Class dialog

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

      the Move Class dialog
    • Click Refactor. As a result, PhpStorm moves Class2 from namespace2\ns2 to namespace3. Accordingly, Class2.php is moved from ...\move_class\namespace2\ns2 to ...\move_class\namespace3:

      Class2 moved
Last modified: 06 August 2021