IntelliJ IDEA 2023.2 Help

Configure PHP namespaces in a project

IntelliJ IDEA comes with a configurable convention, which specifies that the project root folder is also the root for the packages and namespaces. In other words: the project root folder is by default marked as Source and every directory created under it is considered a separate namespace. This complies with the PSR-0, also known as the Autoloading Standard, which prescribes that classes and namespaces in PHP should match the directory and file structure, and vice-versa.

According to PSR-4, any directory can be explicitly assigned a namespace prefix. With this project structure, autoloaders in different PHP frameworks become interoperable.

Install the PHP plugin

This functionality relies on the PHP plugin, which you need to install and enable.

  1. Press Control+Alt+S to open the IDE settings and then select Plugins.

  2. Open the Marketplace tab, find the PHP plugin, and click Install (restart the IDE if prompted).

Detecting namespace roots automatically

When you open a project that contains at least one file with a namespace, IntelliJ IDEA displays a message with a proposition to set the namespace root.

Accordingly, when no namespace root has been configured yet, and you create a class, IntelliJ IDEA proposes to configure the namespace root.

You can also trigger namespace root detection by choosing Code | Detect PSR-0 Namespace Roots from the main menu. The Directories dialog that opens, shows the folders under the project root folder with the project root folder marked as Source, which means that it is the root for all the namespaces in it. Accept the settings by clicking OK or configure the namespace root manually as described below.

Last modified: 24 August 2023