What are PSR (PHP Standard Recommendation) standards, and why is PSR-4 specifically important for modern application structure?
Last updated
PSRs are interoperability standards published by the PHP-FIG (Framework Interop Group) so code from different vendors works together; PSR-4 is the autoloading standard that maps namespaces to file paths, making manual require statements obsolete.
What PSRs are:
Recommendations, not language rules: they cover coding style (
PSR-12), autoloading (PSR-4), logging (PSR-3), HTTP messages (PSR-7), and more.Goal is interoperability: a library following the standards drops into any compliant project.
Why PSR-4 matters:
It defines a deterministic mapping: a fully-qualified class name maps to a directory + file, e.g.
App\Service\Mailer→src/Service/Mailer.php.Composer reads the
autoloadsection and generates an autoloader, so classes load on demand with no manual includes.This is what makes modern app structure (and the whole package ecosystem) work consistently.
[lang:json]
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
- https://en.wikipedia.org/wiki/PHP
- https://lemon.io/for-developers/php-developer-jobs/
- https://lan-portal.uob.edu.ly/visit/BOOK/529X432U02/modern-php_new__features_and_good-practices.pdf
- https://lan-portal.uob.edu.ly/upload/MD/D57953S372/modern-php__new_features_and__good__practices.pdf
- https://news.ycombinator.com/item?id=30786927
- https://kvnc-inc.medium.com/solid-principles-in-php-1c41069e9af6
- https://www.shkodenko.com/
- https://confoo.ca/en/2024/sessions