Glossary · PHP & Backend

What is a PSR (PHP Standards Recommendation)?

Short answer

A PSR (PHP Standards Recommendation) is a specification published by the PHP Framework Interop Group (PHP-FIG) so that PHP libraries and frameworks can work together. PSRs define shared conventions and interfaces, such as autoloading (PSR-4), coding style (PSR-12), logging (PSR-3), HTTP messages (PSR-7) and containers (PSR-11).

The PSRs you will meet most

  • PSR-4: autoloading. Maps namespaces to folders, which is what Composer’s autoload section uses.
  • PSR-12 (now extended by PER Coding Style): code formatting, so code from different teams looks the same.
  • PSR-3: a common LoggerInterface, implemented by Monolog and others.
  • PSR-7 / PSR-17: HTTP request and response objects and their factories.
  • PSR-15: HTTP middleware and request handlers.
  • PSR-11: dependency-injection containers.
  • PSR-18: HTTP clients.

Why they matter

If your code depends on a PSR interface rather than a specific library, you can swap implementations, say one HTTP client for another, without rewriting your code. That is why framework-agnostic packages type-hint Psr\Log\LoggerInterface instead of a concrete logger.

In practice

Use PSR-4 autoloading through Composer, enforce a coding style automatically with PHP-CS-Fixer or PHP_CodeSniffer, and prefer PSR interfaces at the boundaries of reusable code.

Published · Updated · By · All terms

Go deeper