What Are the Differences Between PSR-0 and PSR-4?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Unraveling the Differences Between PSR-0 and PSR-4 for Effective Namespacing
Introduction: Namespaces are fundamental in modern programming. They help avoid naming collisions by grouping classes, interfaces, functions, and constants into logical units. In PHP, there are two widely used standards for class autoloading with namespaces: PSR-0 and PSR-4. The remainder of this blog post will delve deeper into these concepts to help you choose the best fit according to your project needs.
1. Background on PSR-0 and PSR-4
- PSR-0 (PHP Standard Recommendations 0) is a standard for autoloading classes with namespaces that was defined in 2009. It primarily targets PHP 5.3.
- PSR-4 (PHP Standard Recommendations 4) is an updated version of PSR-0, introduced in 2012 to address the shortcomings and complications of PSR-0 while maintaining compatibility with it. It supports PHP 5.3 and above.
2. Autoloading Methodology in Both Standards
- For both PSR-0 and PSR-4, autoloaders are required to take a single argument containing the fully qualified name of the class to be instantiated. The primary difference between them is found in their internal working mechanisms.
3. Differences between PSR-0 and PSR-4
- PSR-0 follows a strict file naming convention for each class, requiring their respective directories to follow specific structures:
- Each namespace must be mapped to its own directory. For example, if your namespace is `My\Custom\Library`, your files would be placed under `lib/custom/library`.
- Class name should be the last part of the file name, with underscores replaced by directory separators. For instance, a class named 'FooBar' would have its corresponding file as 'foo_bar.php'.
- PSR-4 allows more flexibility in naming conventions and removes the need for replacing underscores with directory separators:
- Each namespace must be mapped to its own directory. For example, `My\Custom\Library` would be placed under `lib/custom/library`.
- Class files can have names like 'FooBar', without any file name changes.
4. Pros and Cons of PSR-0 and PSR-4
- PSR-0 is simpler to implement, making it easier for developers to transition from non-namespaced code. However, due to its strict file naming convention, it may lead to repetitive or lengthy class names when dealing with complex projects.
- On the other hand, PSR-4 offers better flexibility and readability in class file naming while maintaining backward compatibility with older PHP versions. It can be more beneficial for larger projects and complex code bases.
5. Choosing the Right Autoloading Standard
- Determine the scale of your project (small, medium, or large) and consider whether you want a simpler implementation process or greater flexibility in class file naming conventions.
- Consider the available resources for each standard, such as popular PHP frameworks like Laravel supporting either one.
- Evaluate the compatibility requirements of your target audience and their PHP version constraints.
Conclusion:
While PSR-0 and PSR-4 primarily differ in naming conventions and level of complexity, both are viable options for autoloading classes with namespaces. Deciding between them requires an understanding of each standard's advantages and disadvantages in the context of your project's size, resource availability, and PHP version compatibility concerns. Incorporating these insights can help you confidently choose the most suitable approach for your project's class autoloading needs.