PhpStorm 2017.3 Help

Creating PHP Classes, Interfaces, and Traits


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 creating classes, interfaces, and traits.

When you specify the folder where the file that implements the new class will be stored, PhpStorm automatically updates the namespace for the class.

Creating PHP classes

  1. Do one of the following:
    • In the Project Tool Window, select the directory in which you want to create a new class/interface/trait, and then choose File | New on the main menu.
    • Right-click the corresponding directory and select New from the context menu.
    • Press Alt+Insert.
  2. Choose PHP Class. The Create New PHP Class dialog box opens.
    ps new class
  3. In the Name text box, type the name of the class/interface/trait to be created. PhpStorm automatically fills in the specified name in the File Name field.
  4. Specify the namespace to create the class/interface/trait in. By default, the Namespace field shows the namespace that corresponds to the folder from which the class/interface/trait creation was invoked.

    You can choose <global namespace> from the drop-down list or change the namespace manually. Keep in mind that this may cause loading problems, since such namespace hierarchy will not meet the PSR0 standards.

    You can also change the folder to store the file that will implement the new class/interface/trait, the change will also be reflected in the Namespace field.

  5. In the Template drop-down list, specify the template from which to create the file. The available options are as follows:
    • The set of PhpStorm bundled templates:
    • Your own set of manually created file templates having the php file extension.
  6. Choose the file extension from the drop-down list.

When you click OK, a new class is created according to the PHP Class template, with the specified namespace declaration automatically added.

Last modified: 29 March 2018

See Also