Working with PHP namespaces
The Import Class Quick Fix
When a class is being used in PHP that has not been imported yet, the IDE will show us a warning about this.
Pressing Alt+Enter will bring up the Import Class quick fix which we can use to let PhpStorm add the correct namespace imports at the top of our file.
When multiple candidates are found, PhpStorm offers us the choice of which class should be imported.
Once finished, our code will have the necessary import added at the top of the file and we get full completion and navigation for the imported type.
Detecting Unused Namespaces
When referencing namespaces and classes that are not used by our code, PhpStorm will let us know using a warning.
We can remove the import manually or use #Optimizing Imports to let PhpStorm do it for us.
We may be importing a number of namespaces that we aren't actually using. And when importing many, we may want to rearrange them so they are grouped and ordered alphabetically. We can optimize imports in the current file or in our entire project: pressing Ctrl+Alt+O (Alt+CMD+O on Mac OS X) brings up the Optimize Imports dialog where we can provide PhpStorm with some additional settings.
Once we click Run, PhpStorm will, depending on the selected options in the dialog, do the following things:
- Remove unused imports
- Sort imports alphabetically (ascending order)
- Split multiple use statement imports into single use statement imports
Working with PSR-0 and PSR-4 Namespace Roots
PSR-0, also known as the Autoloading Standard, prescribes that classes and namespaces in PHP should match the directory and file structure, and vice-versa. In other words: every directory created under the Source Root should be considered a separate namespace. PSR-4 allows specifying a namespace prefix for a given directory explicitly. Having this project structure makes autoloaders in different PHP frameworks interoperable.
When opening a PhpStorm project that contains at least one file with a namespace, the IDE will propose setting the namespace root. Also, when no configuration has been provided yet and a class is created, the IDE will propose to configure the namespace root.
Detection of the source root can also be triggered from the Code | Detect PSR-0 Namespace Roots menu or through Project Settings | Directories.
We can configure multiple source roots: PhpStorm will treat each of those as a namespace root and provide namespace hints for files underneath them.
Through Project Settings | Directories, we can configure the package prefix according to PSR-4. We can specify the namespace for every source root by clicking the p icon next to a source folder.
When configured (typically once per project), these settings are used throughout the IDE. For example when creating a new class or a new PHPUnit test class as well as in structure refactorings (Move Class, Move Namespace, Extract Interface), the IDE will know about the configured namespaces.
Detecting Namespaces not following the PSR-0 or PSR-4 Configuration
PhpStorm comes with an inspection to verify that all namespaces comply to the PSR-0 and PSR-4 configuration: if the class path does not match the namespace, PhpStorm will tell us.
Working with XML namespaces
When working with XML, we can use elements from various namespaces in our document. We can start writing and will see that the Create Namespace Declaration inspection and quick fix will highlight namespaces that we did not bind to our document yet.
We can press Alt+Enter and invoke the quick fix.
PhpStorm will add a namespace import to our document's root tag where we can provide the URI.