Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Concepts and Reference

PHP PSR standards

What the PHP-FIG PSR standards are and which matter most: PSR-1 and PSR-12 coding style, PSR-4 autoloading, PSR-3 logging, PSR-7 and PSR-15 HTTP interfaces.

A PSR (PHP Standards Recommendation) is a document published by the PHP-FIG, a group of framework and library authors. PSRs are not part of the language. They are agreements that let separate libraries work together: one package can log through any logger, load classes the same way as every other package, and be formatted the same way.

You meet them daily without noticing. Composer's autoloading follows PSR-4, and Laravel, Symfony and most packages follow PSR-12 style.

PHP
<?php
// PSR-4: namespace prefix App\ maps to the folder src/
//   App\Billing\Invoice  ->  src/Billing/Invoice.php

Try it

PHP PSR-4 in miniature: namespace to file path

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP PSR-3 style logging through an interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

The standards that matter

PSR Topic What it gives you
PSR-1 Basic coding standard <?php tags, UTF-8, StudlyCaps classes, camelCase methods, UPPER_CASE constants, no side effects mixed with declarations
PSR-12 Extended coding style (replaces PSR-2) Indentation, braces, line length, use ordering, one statement per line
PSR-4 Autoloading Namespace prefix to directory mapping, one class per file
PSR-3 Logger interface LoggerInterface with info(), error() and so on, and interpolation
PSR-6, PSR-16 Caching Pool/item interface and a simple get/set interface
PSR-7, PSR-17, PSR-18 HTTP messages, factories and client Immutable request/response objects, a way to create them, a client interface
PSR-11 Container ContainerInterface with get() and has()
PSR-14 Event dispatcher Dispatcher and listener provider interfaces
PSR-15 HTTP middleware Request handler and middleware interfaces
PSR-20 Clock ClockInterface::now() so time can be faked in tests

PSR-0 (old autoloading), PSR-2 (old style) and PSR-5/PSR-19 (docblock drafts) are deprecated or never accepted. Use PSR-4 and PSR-12.

Usage notes

  • Interface PSRs ship as small Composer packages (psr/log, psr/http-message, psr/container) that you require and implement. They contain no behavior.
  • PSR-4 with Composer: put "autoload": {"psr-4": {"App\\": "src/"}} in composer.json, then run composer dump-autoload.
  • File and class names must match exactly, including case. A class that works on Windows can fail on a Linux server if the case differs.
  • Pint, PHP_CodeSniffer and PHP CS Fixer enforce PSR-12 automatically.
  • Following a PSR is optional, but a package that does is far easier to swap or combine.