What are PHP's PSRs?

October 30, 2020

PSRs are PHP Standards Recommendations. They are a standard and clearly defined way to do many common tasks. Here is a guide to all of the PHP PRSs.

Some common PSRs:

PSR-0 - Autoloading Standard

This is not in use any more - you should use PSR-4.

It describes the mandatory requirements that must be adhered to for autoloader interoperability.

PSR-1 - Basic Coding Standard

It comprises what should be considered the standard coding elements that are required to ensure a high level of technical interoperability between shared PHP code.

PSR-2 - Coding Style Guide

It considers PSR-1 and it is intended to reduce cognitive friction when scanning code from different authors. It does so by enumerating a shared set of rules and expectations about how to format PHP code.

Some of the basic and main rules:

  • Use spaces, not tabs (4 spaces = 1 tab)
  • The namespace ... should be on its own line
  • The closing ?> must not be present, for files containing only PHP
  • PHP keywords (true, false, null) must always be in lower case
  • The extends and implements must be on the same line as the class name
  • The opening bracket must go on its own (new) line
  • You must always provide a public/protected/private for all object properties. Same for methods.
  • There must be exactly one space after the comma between method or function parameters, and no space before the comma
  • Function and method parameters may be spread over several lines, if you wish
  • And if structures must look like the following:
if ($expr1) {
    // if body
} elseif ($expr2) {
    // elseif body
} else {
    // else body;

You can find the full specs here.

PSR-3 - Logger Interface

It describes a common interface for logging libraries.

PSR-4 - Autoloading Standard

It describes a specification for autoloading classes from file paths. It is fully interoperable, and can be used in addition to any other autoloading specification, including PSR-0. This PSR also describes where to place files that will be auto loaded according to the specification.

PSR-4 is often used with Composer's composer.json to help with autoloading of PHP classes.

PSR-5 - PHPDoc Standard

This spec is only a draft (as of Oct 2020)

The main purpose of this PSR is to provide a complete and formal definition of the PHPDoc standard. This PSR deviates from its predecessor, the de facto PHPDoc Standard associated with phpDocumentor 1.x, to provide support for newer features in the PHP language and to address some of the shortcomings of its predecessor. Is only a draft (as of Oct 2020)

PSR-6 - Caching Interface

The goal of this PSR is to allow developers to create cache-aware libraries that can be integrated into existing frameworks and systems without the need for custom development.

Here are the interfaces - I've removed most of the comments to make this page more readable, but if you want to see full details then please go here.


CacheItemInterface defines an item inside a cache system.

namespace Psr\Cache;
// CacheItemInterface defines an interface for interacting with objects inside a cache.
interface CacheItemInterface
     // Returns the key for the current cache item.
    public function getKey();
     // Retrieves the value of the item from the cache associated with this object's key.
    public function get();
     // Confirms if the cache item lookup resulted in a cache hit.
    public function isHit();
     // Sets the value represented by this cache item.
    public function set($value);
     // Sets the expiration time for this cache item.
    public function expiresAt($expiration);
     // Sets the expiration time for this cache item.
    public function expiresAfter($time);


The primary purpose of Cache\CacheItemPoolInterface is to accept a key from the Calling Library and return the associated Cache\CacheItemInterface object.

namespace Psr\Cache;
// CacheItemPoolInterface generates CacheItemInterface objects.
interface CacheItemPoolInterface
     // Returns a Cache Item representing the specified key.
    public function getItem($key);
     // Returns a traversable set of cache items.
    public function getItems(array $keys = array());
     // Confirms if the cache contains specified cache item.
    public function hasItem($key);
     // Deletes all items in the pool.
    public function clear();
     // Removes the item from the pool.
    public function deleteItem($key);
     // Removes multiple items from the pool.
    public function deleteItems(array $keys);
     // Persists a cache item immediately.
    public function save(CacheItemInterface $item);
     // Sets a cache item to be persisted later.
    public function saveDeferred(CacheItemInterface $item);
     // Persists any deferred cache items.
    public function commit();

CacheException and InvalidArgumentException

There are two interfaces for exceptions related to PSR-6 that should be used.

CacheException: Exception interface for all exceptions thrown by an Implementing Library

InvalidArgumentException: Exception interface for invalid cache arguments.

PSR-7 explained: HTTP Message Interface

What is PSR-7, and when to use it? PSR 7 is a set of interfaces for HTTP messages and URIs, used when PHP is used as a web server (i.e. over HTTP).

If you want to get an instance of a PSR-7 request in Laravel, then you must use the symfony/psr-http-message-bridge and zendframework/zend-diactoros package. Then you can get a Psr\Http\Message\ServerRequestInterface class automatically injected into your controller methods by type hinting the ServerRequestInterface interface as a method param.

PSR 7 basically describes common interfaces for representing HTTP messages as described in RFC 7230 and RFC 7231, and URIs for use with HTTP messages as described in RFC 3986.

A common package used is the Zend Diactoros one (which is also the one recommended to use if you want to use PSR-7 requests with Laravel. Find more details here.

PSR-9 - Security Disclosure

This spec was abandoned

It gives project leads a clearly dIt gives project leads a clearly defined approach to enabling end users to discover security disclosures using a clearly defined structured format for these disclosures.

PSR-10 - Security Advisories

This spec was abandoned

It gives researchers, project leads, upstream project leadIt gives researchers, project leads, upstream project leads and end users a defined and structured process for disclosing security vulnerabilities.

PSR-11 - Container Interface

It describes a common interface for dependency injection containers. The goal is to standardize how frameworks and libraries make use of a container to obtain objects and parameters (called entries in the rest of this document).

PSR-12 - Extended Coding Style Guide

It extends, expands and replaces PSR-2, the coding style guide and requires adherence to PSR-1, the basic coding standard.

PSR-13 describes common interfaces for representing a hypermedia link.

PSR-14 - Event Manager

PSR-14 describes common interfaces for dispatching and handling events

PSR-15 - HTTP Server Request Handlers

PSR-15 describes common interfaces for HTTP server request handlers and HTTP server middleware components that use HTTP messages.

PSR-16 - Simple Cache

PSR 16 describes a simple yet extensible interface for a cache item and a cache driver

Source: Wikipedia's list of all PSRs