Skip to main content

25. PSR & PER Standards

The PHP Framework Interop Group (PHP-FIG) develops PHP Standard Recommendations (PSR) and PHP Evolved Recommendations (PER) to help developers write interoperable code.

What PSR documentation is covering Basic Coding Standards?​

Junior

Answer: PSR-1 (link) PSR-1 aims to ensure a high degree of technical interoperability between shared PHP code. It covers basic coding elements such as files, namespaces, classes, and methods.

  • Use only <?php and <?= tags.
  • Use only UTF-8 without BOM for PHP code.
  • Files should either declare symbols (classes, functions, constants, etc.) or cause side effects (generate output, change .ini settings, etc.) but should not do both.
  • Namespaces and classes must follow PSR-4.
  • Class names must be declared in StudlyCaps.
  • Class constants must be declared in all upper case with underscore separators.
  • Method names must be declared in camelCase.

What PSR documentation is covering Extended Coding Standards?​

Junior

Answer: PSR-12 (link) PSR-12 is an extension of PSR-2 (which it superseded) and provides a more comprehensive set of coding style rules. Note: It has been superseded by the living PER Coding Style standard.

  • Indentation must be 4 spaces, no tabs.
  • Line length soft limit is 80 characters, hard limit 120.
  • Braces for classes and methods must go on a new line.
  • Visibility must be declared on all properties and methods.
  • Type hints are required for parameters and return values where possible.

What PSR documentation is covering Autoloading?​

Junior

Answer: PSR-4 (link) PSR-4 describes a specification for autoloading classes from file paths. It is the modern replacement for PSR-0.

  • It maps namespaces to directory structures.
  • It allows for a "prefix" (base directory) for a namespace.
  • Note on PSR-0: PSR-0 was the previous standard but was deprecated because it was less flexible (e.g., it required the full namespace to be reflected in the directory structure, and underscores in class names were treated as directory separators).

What PSR documentation is covering Simple Cache?​

Junior

Answer: PSR-16 (link) A simplified caching interface focused on basic get, set, delete, and has operations for simple use cases.


What PSR documentation is covering the Clock interface?​

Junior

Answer: PSR-20 (link) Introduces a ClockInterface to obtain the current time, allowing time to be injected for deterministic tests and time-based logic.


What PSR documentation is covering Logging Interface?​

Middle

Answer: PSR-3 (link) PSR-3 provides a common interface for logging libraries. It defines a LoggerInterface with methods for 8 log levels (debug, info, notice, warning, error, critical, alert, emergency). This allows applications to be independent of the specific logging implementation (like Monolog).


What PSR documentation is covering HTTP Message Interface?​

Middle

Answer: PSR-7 (link) PSR-7 describes common interfaces for representing HTTP messages (Requests and Responses).

  • HTTP messages are treated as immutable objects.
  • Covers requests, responses, uploaded files, URIs, and streams.
  • Facilitates interoperability between different HTTP-related libraries and frameworks.

What PSR documentation is covering HTTP Handlers?​

Middle

Answer: PSR-15 (link) PSR-15 defines interfaces for HTTP server-side components: Request Handlers and Middleware.

  • Middleware allows processing incoming requests and outgoing responses in layers.
  • Works in conjunction with PSR-7 HTTP messages to provide a standard way of handling web requests.

What PSR documentation is covering Caching Interfaces?​

Middle

Answer: PSR-6 (link) Provides common interfaces for caching libraries (cache pools, items, and drivers), enabling applications to swap implementations without code changes.


Middle

Answer: PSR-13 (link) Defines interfaces to represent web links (relations between resources), usable alongside PSR-7/PSR-18 libraries.


What PSR documentation is covering HTTP Client?​

Middle

Answer: PSR-18 (link) Standard HTTP Client interface to send PSR-7 requests and receive PSR-7 responses, decoupling apps from concrete HTTP clients.


What PSR documentation is covering Container interface?​

Senior

Answer: PSR-11 (link) PSR-11 provides a common interface for dependency injection containers.

  • Defines get($id) and has($id) methods.
  • Allows libraries to retrieve entries from a container in a standardized way, making them container-agnostic.

What PSR documentation is covering Event Dispatcher?​

Senior

Answer: PSR-14 (link) PSR-14 defines a common interface for dispatching events and listening for them.

  • Consists of an Event Dispatcher, a Listener Provider, and the Event object.
  • Provides a standard way to implement extension points in PHP applications and libraries.

What PSR documentation is covering HTTP Factory?​

Senior

Answer: PSR-17 (link) PSR-17 defines interfaces for factories that create PSR-7 compatible HTTP objects.

  • Since PSR-7 objects are immutable, factories provide a standard way to create new instances (Requests, Responses, Streams, etc.) without depending on a specific implementation.

What is PER Coding Style and why is it important?​

Senior Important

Answer: PER Coding Style (link) The PHP Evolved Recommendation (PER) for Coding Style is the modern successor to PSR-12. Unlike PSRs, which are static once accepted, PERs are "living documents" that can be updated to reflect new PHP features and evolving best practices.

  • Successor to PSR-12: It expands and replaces PSR-12 while still requiring adherence to PSR-1 (Basic Coding Standards).
  • Living Standard: It is updated to include rules for new PHP syntax such as constructor property promotion, enums, readonly properties, and more.
  • Goal: To provide a comprehensive, evolving set of coding style rules that keep pace with the language's development, ensuring consistent code across the PHP ecosystem.

Summary of other PSR Standards​

Below are additional PSRs with short descriptions and links.

What PSR standards are deprecated or drafts?​

  • PSR-0: Autoloading Standard — Deprecated in favor of PSR-4.
  • PSR-2: Coding Style Guide — Deprecated in favor of PSR-12.
  • PSR-12: Extended Coding Style — Superseded by PER Coding Style.
  • PSR-5: PHPDoc Standard (Draft) — Proposed rules for docblocks (draft).
  • PSR-19: PHPDoc tags (Draft) — Proposed set of PHPDoc tags (draft).
  • PSR-21: Internationalization (Draft) — Proposed i18n interfaces (draft).
  • PSR-22: Application Tracing (Draft) — Proposed tracing interfaces (draft).