Skip to end of metadata
Go to start of metadata

The license could not be verified: License Certificate has expired!


Many programming languages have the concept of namespaces to group logical sets of classes or elements. In this tutorial, we'll have a look at some of the features PhpStorm provides around namespaces.

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.

Optimizing Imports

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:

  1. Remove unused imports
  2. Sort imports alphabetically (ascending order)
  3. Split multiple use statement imports into single use statement imports

When using the Code | Reformat Code... menu, which reformats the layout of our code to the configured settings, we can tick the "Optimize Imports" checkbox to also optimize imports in one go.

It's also possible to tick the Optimize Imports checkbox when committing code to source control, so that optimize imports is performed before the actual commit and cleaner code ends up in source control.

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.

PSR-0 support

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.

PSR-4 support

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.


The IDE will provide completion (Ctrl+Space) on XML namespace URI's that are registered through Project Settings | Schemas and DTDs. When using known schemas and DTD's, the IDE will also let us import the correct namespace at once.


  • No labels