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.
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.
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.
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.
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.
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.
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
Subscribe to Blog updates
Thanks, we've got you!
PhpStorm 2023.2 Beta
In this build, we’ve added new inspections for PHP assertions and revised the integration with third-party quality tools.
PhpStorm 2023.2 EAP #6
This build brings improvements for generating PHPDocs, support for Laravel Pint, a single point of access to all PHP framework plugins, and much more!
AI Features in PhpStorm: PhpStorm 2023.2 EAP #5
The next EAP build for PhpStorm 2023.2 is now available, and it’s an exciting one! It is the first build to include features leveraging the enormous potential of generative AI and large language models (LLMs). Our Early Access Program (EAP) allows you to try new features from the upcoming PhpStor…
PhpStorm 2023.2 EAP #4
This build includes a bunch of usability improvements, such as abilities to pin Run configurations and sort files by modification time in the Project view. The EAP allows you to try new features from the upcoming PhpStorm version, 2023.2. EAP builds are free to use and you can install them side b…