Hướng dẫn auto import class phpstorm

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 .

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

    • To have PhpStorm automatically add use statements for classes and methods in pasted blocks of code, choose the desired behavior from the Insert imports on paste list:

      • All: import statements will be added automatically for all missing classes and methods found in pasted blocks of code.

      • Ask: PhpStorm will prompt you to select which classes and methods you want to import.

        If the pasted class is already referenced in the target code via an alias, PhpStorm will prompt you to reuse this alias instead of creating a new import statement.

        Hướng dẫn auto import class phpstorm
      • None: no import statements will be added, you won't be asked about unresolved references.

      Note that adding imports on paste is only possible if the copied element is properly resolved in code (that is, not highlighted by the Undefined class or Undefined method inspections), and project indexing is finished.

    • 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;

Disable auto import

If you want to completely disable auto-import, make sure that:

  • All import tooltips are disabled.

  • The automatic insertion of import statements is disabled.

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.

    Hướng dẫn auto import class phpstorm

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:

    Hướng dẫn auto import class phpstorm
  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).

    Hướng dẫn auto import class phpstorm

Shorten fully qualified class names with Code Cleanup

PhpStorm provides the following inspections and quick-fixes for shortening fully qualified class names:

  • Fully qualified name usage inspection highlights the fully qualified class names that can be removed by adding a use statement.

  • Unnecessary fully qualified name inspection highlights the fully qualified class names that can be removed without adding a use statement.

You can apply the corresponding quick-fixes to a given scope automatically by using Code Cleanup.

Clean up code on a given scope

  1. From the main menu, select .

  2. In the Specify Code Cleanup Scope dialog that opens, select the scope to which you want the inspection profile to be applied.

  3. Select the inspection profile from the list, or click to configure a new profile in the Code Cleanup Inspections dialog that opens. You can also click to check which fixes will be applied and make sure that the Unnecessary fully qualified name and Fully qualified name usage inspections are enabled.

    Hướng dẫn auto import class phpstorm
  4. Click OK to launch code cleanup.

Clean up code in the current file

  1. In the editor, position the caret at a fully qualified class name highlighted by the Unnecessary fully qualified name or Fully qualified name usage inspection.

  2. Click or press Alt+Enter, and select Cleanup code from the popup menu.

Hướng dẫn auto import class phpstorm

Optimize imports

The Optimize Imports feature helps you remove unused imports and organize import statements in the current file or in all files in a directory at once according to the rules specified in .

Optimize all imports

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

  2. Do any of the following:

    • From the main menu, select (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 your project is under version control), and click Run.

When optimizing imports, PhpStorm can automatically sort the use statements either alphabetically or by their length. To choose the preferred option, in the Settings/Preferences dialog (Ctrl+Alt+S), go to and switch to the Code Conversion tab. Then select the Sort 'use' statements checkbox and choose how the use statements should be sorted.

Optimize imports in a single file

  1. Place the caret at the import statement and press Alt+Enter or use the icon.

  2. Select Remove use statement.

Hướng dẫn auto import class phpstorm

Optimize imports when committing changes to Git

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

  1. Press Ctrl+K or select from the main menu.

  2. Click and in the Before commit area, select the Optimize imports checkbox.

Automatically optimize imports on save

You can configure the IDE to optimize imports in modified files automatically when your changes are saved.

  1. Press Ctrl+Alt+S to open the IDE settings and select .

  2. Enable the Optimize imports option.

    Additionally, from the All file types list, select the types of files in which you want to optimize imports.

  3. Apply the changes and close the dialog.

Optimize imports when reformatting a file

You can tell PhpStorm to optimize imports in a file every time it is reformatted.

  • Open the file in the editor, press Ctrl+Alt+Shift+L, and make sure the Optimize imports checkbox is selected in the Reformat File dialog that opens.

    Hướng dẫn auto import class phpstorm

After that every time you press Ctrl+Alt+L in this project, PhpStorm will optimize its imports automatically.

Last modified: 01 August 2022