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 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. 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 PhpStorm automatically add 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. 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.
Disable auto importIf you want to completely disable auto-import, make sure that:
Import a PHP namespace on-the-fly
Import a class by using a quick fix
Shorten fully qualified class names with Code CleanupPhpStorm provides the following inspections and quick-fixes for shortening fully qualified class names:
You can apply the corresponding quick-fixes to a given scope automatically by using Code Cleanup. Clean up code on a given scope
Clean up code in the current file
Optimize importsThe 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
When optimizing imports,
PhpStorm can automatically sort the Optimize imports in a single file
Optimize imports when committing changes to GitIf your project is under version control, you can instruct PhpStorm to optimize imports in modified files before committing them to VCS.
Automatically optimize imports on saveYou can configure the IDE to optimize imports in modified files automatically when your changes are saved.
Optimize imports when reformatting a fileYou can tell PhpStorm to optimize imports in a file every time it is reformatted.
After that every time you press Ctrl+Alt+L in this project, PhpStorm will optimize its imports automatically. Last modified: 01 August 2022 |