Skip to main content

The PHP Standard Recommendation (PSR)

Updated by Tim Rabbetts on
php, elephant, blue elephant

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interop Group. Similar to Java Specification Request for Java, it serves the standardization of programming concepts in PHP. The aim is to enable the interoperability of components and to provide a common technical basis for the implementation of proven concepts for optimal programming and testing practices. The PHP-FIG is formed by several PHP frameworks founders.

Each PSR is suggested by members and voted according to an established protocol to act consistently and in line with their agreed-upon processes.

PSR-0 Autoloading Standard Deprecated and substituted by PSR-4[4]

PSR-1 Basic Coding Standard    

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

This guide intentionally avoids any recommendation regarding the use of $StudlyCaps$camelCase, or $snake_case property names.  I use a snake case for variables/properties which is pretty standard within the Drupal 8 codebase.

PSR-2 Coding Style Guide Deprecated

PSR-3 Logger Interface

It describes a common interface for logging libraries.

Drupal 8 implements the PSR-3 Logger Interface Standard by default.  PSR-3 Watchdog is a wrapper module for drupal 7's watchdog logger that implements the PSR-3 Logger Interface.  Standard log levels are EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO and DEBUG.

PSR-4    Autoloading Standard  

This PSR 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 autoloaded according to the specification.

PSR-5 PHPDoc Standard (DRAFT)

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.

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.

PSR-7 HTTP Message Interface

It 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.

PSR-8 Huggable Interface Abandoned    

It establishes a common way for objects to express mutual appreciation and support by hugging. This allows objects to support each other in a constructive fashion, furthering cooperation between different PHP projects.

PSR-9 Security Disclosure Abandoned

PSR-10 Security Advisories Abandoned

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

Depenency injection is great stuff, used within Drupal 8 with services, see this article for a guide on creating services in Drupal 8.

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. 

Drupal has its own coding standards, this article contains details on Drupals coding standards and how to setup phpcs with sublime to adhear to them.

PSR-13 Hypermedia Links

It describes common interfaces for representing a hypermedia link.

Hypermedia links directly relate to REST APIs.  It's where the responses from the REST API contain links that help access the API.  This is a great article about hypermedia links.  It's only any good if the client knows and uses the hypermedia links.  API responses usually take the form of HAL (Hypertext Application Language).  If you have set up a REST API using Drupal 8 you will be familiar with this sort of response, _links demonstrates hypermedia links:

{
  "_links": {
    "self": {
      "href": "http://example.com/api/book/hal-cookbook"
    }
  },
  "id": "hal-cookbook",
  "name": "HAL Cookbook"
}

PSR-14 Event Manager

It describes common interfaces for dispatching and handling events.  

In Drupal 7 hooks are used for events.  In Drupal 8, there is a new event system, read create event subscribers and dispatches article.

PSR-15 HTTP Server Request Handlers

It describes common interfaces for HTTP server request handlers and HTTP server middleware components that use HTTP messages.

PSR-16 Simple Cache

It describes a simple yet extensible interface for a cache item and a cache driver.

PSR-17 HTTP Factories

It describes a common standard for factories that create PSR-7 compliant HTTP objects.

PSR-18 HTTP Client    

It describes a common interface for sending HTTP requests and receiving HTTP responses.

PSR-19 PHPDoc tags (DRAFT)

It provides a complete catalog of tags in the PHPDoc standard.

Add new comment