What are PHP's PSRs?

PHP: What are PHP's PSRs?

PSRs are PHP Standards Recommendations. They are a standard and clearly defined way to do many common tasks.

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:
  1. <?php
  2. if ($expr1) {
  3.     // if body
  4. } elseif ($expr2) {
  5.     // elseif body
  6. } else {
  7.     // else body;
  8. }

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 2018)

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 2018)

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.

  1. <?php
  3. namespace Psr\Cache;
  5. // CacheItemInterface defines an interface for interacting with objects inside a cache.
  6. interface CacheItemInterface
  7. {
  8.      // Returns the key for the current cache item.
  9.     public function getKey();
  11.      // Retrieves the value of the item from the cache associated with this object's key.
  12.     public function get();
  14.      // Confirms if the cache item lookup resulted in a cache hit.
  15.     public function isHit();
  17.      // Sets the value represented by this cache item.
  18.     public function set($value);
  20.      // Sets the expiration time for this cache item.
  21.     public function expiresAt($expiration);
  23.      // Sets the expiration time for this cache item.
  24.     public function expiresAfter($time);
  26. }

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

  1. <?php
  3. namespace Psr\Cache;
  5. // CacheItemPoolInterface generates CacheItemInterface objects.
  6. interface CacheItemPoolInterface
  7. {
  8.      // Returns a Cache Item representing the specified key.
  9.     public function getItem($key);
  11.      // Returns a traversable set of cache items.
  12.     public function getItems(array $keys = array());
  14.      // Confirms if the cache contains specified cache item.
  15.     public function hasItem($key);
  17.      // Deletes all items in the pool.
  18.     public function clear();
  20.      // Removes the item from the pool.
  21.     public function deleteItem($key);
  23.      // Removes multiple items from the pool.
  24.     public function deleteItems(array $keys);
  26.      // Persists a cache item immediately.
  27.     public function save(CacheItemInterface $item);
  29.      // Sets a cache item to be persisted later.
  30.     public function saveDeferred(CacheItemInterface $item);
  32.      // Persists any deferred cache items.
  33.     public function commit();
  34. }
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 is only a draft (as of Oct 2018)

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. Is only a draft (as of Oct 2018)

PSR-10 - Security Advisories
This spec is only a draft (as of Oct 2018)

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. Is only a draft (as of Oct 2018)

PSR-11 - Container Interface
This spec is only a draft (as of Oct 2018)

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). Is only a draft (as of Oct 2018)

PSR-12 - Extended Coding Style Guide
This spec is only a draft (as of Oct 2018)

It extends, expands and replaces PSR-2, the coding style guide and requires adherence to PSR-1, the basic coding standard. Is only a draft (as of Oct 2018)

Source: Wikipedia's list of all PSRs

webdevetc profile pic

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.


Comments and discussion about What are PHP's PSRs?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

What are PHP's PSRs?

How to use subdomains as parameters in your routes (or in your web.php file)

What does 'final class' and 'final function' (final method) mean in PHP?

How to block multiple IP addresses in .htaccess

How do you access the php://input stream?

PHP Date commonly used formats

What are the SPL Iterators in PHP?

What is the null coalescing operator in PHP7?

What is the difference between var, let and const in Javascript?

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?