Early Access Program Features Releases

PSR-0, PSR-4 and Source/Test Root support in PhpStorm 8 EAP

The latest PhpStorm 8 EAP brings support for PSR-0 and PSR-4 namespace roots. PhpStorm comes with a configurable convention which specifies that the project Source Root is also a root for the package and namespace. In other words: every directory created under the Source Root will be considered a separate namespace.

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. 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.

Autodetection of PSR-0 root

Detection of the source root can also be triggered from the Code | Detect PSR-0 Namespace Roots menu. We can also configure the source root manually through Project Settings | Directories.

Detection of PSR-0 root

It is perfectly fine to configure multiple source roots: PhpStorm will treat each of those as a namespace root and provide namespace hints for files underneath them. For example every subfolder of a vendor folder can be marked as a namespace root. In the example below, we have two namespace roots: one for the actual application and one for a vendor folder.

Multiple PSR-0 roots

Note that next to PSR-0, we can also configure the package prefix according to PSR-4. This can be done by clicking the p icon next to a source folder. PhpStorm will use the prefix specified here as the namespace root for the source folder.

PSR-4 namespace prefix support

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, and in structure refactorings (Move Class, Move Namespace, Extract Interface), to provide namespace hints.

Create class respecting PSR-0

There is also an inspection to verify that all namespaces comply to the PSR-0 and PSR-4 settings. If the class path does not match the namespace, PhpStorm will tell us.

PSR-0, PSR-4 inspection namespace does not match project structure

Download PhpStorm 8 EAP and give it a go! Note this is still a preview and we are working on expanding this feature. We very much appreciate your feedback through the issue tracker, or by posting your comments below or in our forums!

Develop with pleasure!
– JetBrains PhpStorm Team

image description