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.
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.
Automatically add import statements
You can configure the IDE to automatically add import statements if there are no options to choose from.
In the Settings/Preferences dialog (Ctrl+Alt+S), click .
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
usestatements 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
usestatement. 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
Open the desired file for editing and start typing the short name of a class.
From the code completion suggestion list, select the desired class name. PhpStorm will complete the short class name and insert the
usestatement with the namespace where the selected class is defined.
Import a class by using a quick fix
Open a file for editing and reference a PHP class. If the referenced class is not bound, PhpStorm will highlight it:
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 (
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.
Select a file or a directory in the Project tool window.
Do any of the following:
From the main menu, select(or press Ctrl+Alt+O).
From the context menu, select Optimize Imports.
(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
Place the caret at the import statement and press Alt+Enter (or use the intention action icon).
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.
From the main menu, select(or press Ctrl+K).
In the Before commit area of the Commit Changes dialog, select the Optimize imports checkbox.