PhpStorm 2018.2 Help

Creating Imports

In this section:

Introduction

When you reference a class that has not been imported yet, PhpStorm suggests to add the corresponding use statement or even adds it automatically on the fly, when you complete your code.

The import statement is added to the imports section, but the cursor does not move from the current position, and your current editing session does not suspend. This feature is known as the Import Assistant.

The same possibility applies to the XML files. When you type a tag with an unbound namespace, the import assistant suggests to create a namespace and offers a list of appropriate choices.

When you reference a PHP class that is defined outside the current file, PhpStorm locates the class definition, whereupon you can do one of the following:

  • Have PhpStorm automatically complete the fully qualified class name, including the namespace the class is defined in. This will result in littering your code.

  • Have PhpStorm automatically complete the short class name and import the namespace the class is defined in.

  • Import the namespace manually using a quick fix.

In the PHP context, a namespace is imported by inserting a use statement.

Completing a short class name and importing a PHP namespace on-the-fly

To compile a short class name and import a PHP namespace, follow these steps:

  1. To enable on-the-fly namespace import, open the PhpStorm settings, and then click Auto Import under the Editor node. In the Editor: Auto Import page that opens, configure automatic namespace import in the PHP section.
    • To have PhpStorm automatically import PHP namespaces, add use statements, and complete short class names on the fly when you are typing in a class or file that belongs to a certain namespace, select the Enable auto-import in namespace scope check box. This check box is selected by default.

    • To have automatic namespace import applied when you are typing in a file that does not belong to any specific namespace, select the Enable auto-import in file scope check box.

  2. Open the desired file for editing and start typing the short name of a class.

  3. From the suggested variants for completion, select the relevant class name:

    ps_complete_short_class_name_select_class.png
    PhpStorm completes the short class name and inserts a use statement with the namespace where the selected class is defined:
    ps_complete_short_class_name_use_statement_added.png

Importing a PHP namespace using a quick fix

To import a PHP class using a quick fix, follow these steps:

  1. Open the desired file for editing and reference a PHP class. If the referenced class is not bound, the following prompt appears:

    ps_undefined_class.png

  2. Press Alt+Enter. PhpStorm suggests to import the namespace where it has detected the declaration of the class:

    ps_add_use_statement.png

  3. Press Enter. PhpStorm inserts a namespace declaration statement (use statement):

    ps_undefined_class_use_statement_added.png

Last modified: 21 November 2018

See Also