PhpStorm 2018.3 Help

Using auto import

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

  • Automatically complete the fully qualified class name, including the namespace the class is defined in.

  • Automatically complete the short class name and import the namespace the class is defined in.

  • Import the namespace manually using a quick fix.

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

In JavaScript and TypeScript files, PhpStorm automatically adds import statements for modules, classes, components, and any other symbols that can be exported, as well as for XML namespaces. Learn more from Auto import in JavaScript, Auto import in TypeScript and Importing an XML namespace.

Automatically add import statements

You can configure the IDE to automatically add import statements if there are no options to choose from.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), click Editor | General | Auto Import.

  2. In the PHP section, configure automatic namespace import.

    • 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 checkbox.

    • 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 checkbox. This checkbox is selected by default.

    • If necessary, configure auto-import from the global namespace separately for classes, functions, and constants.

      • Prefer FQN: If selected, PhpStorm automatically inserts the fully-qualified name of a symbol from the global namespace, prepended with a backslash, for example:

        namespace A; $myException = new \Exception(); $a = \strlen("Test"); echo \PHP_EOL;
      • Prefer Import: If selected, PhpStorm automatically adds use statements for symbols from the global namespace if this doesn’t result in a conflict, for example:

        namespace A; use Exception; use const PHP_EOL; use function strlen; $myException = new Exception(); $a = strlen("Test"); echo PHP_EOL;
      • Prefer Fallback: If selected, PhpStorm neither inserts a fully-qualified name of a function or a constant, nor imports them by means of the use statement. The fallback global functions or constants are preferred in this case, for example:

        namespace A; use Exception; $myException = new Exception(); $a = strlen("Test"); echo PHP_EOL;

Import a PHP namespace on-the-fly

  1. Enable on-the-fly namespace import.

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

  3. From the code completion suggestion list, select the desired class name. PhpStorm will complete the short class name and insert the use statement with the namespace where the selected class is defined.

Import a class by using a quick fix

  1. Open a file for editing and reference a PHP class. If the referenced class is not bound, PhpStorm will highlight it:

  2. Press Alt+Enter and accept the suggestion to import the namespace where the declaration of the class is detected.

    PhpStorm inserts a namespace declaration statement (use statement).

Optimize imports

The Optimize Imports feature helps you remove unused imports, add missing imports, and organize import statements in the current file, or in all files in a directory at once.

  1. Select a file or a directory in the Project tool window.

  2. Do any of the following:

    • From the main menu, select Code | Optimize Imports (or press Ctrl+Alt+O).

    • From the context menu, select Optimize Imports.

  3. (If you've selected a directory) Choose whether you want to optimize imports in all files in the directory, or only in locally modified files (if yor project is under version control), and click Run.

Optimize imports in a single file

  1. Place the caret at the import statement and press Alt+Enter (or use the intention action the Intention action button icon).

  2. Select Remove use statement.

Automatically optimize imports in modified files

If your project is under version control, you can instruct PhpStorm to optimize imports in modified files before committing them to VCS.

  1. From the main menu, select VCS | Commit (or press Ctrl+K).

  2. In the Before commit area of the Commit Changes dialog, select the Optimize imports checkbox.

Last modified: 18 March 2019