PSR Standards in PHP - Comprehensive Guide

20.08.2025

PHP 8.4 Course in Practice – PSR Standards

PSR (PHP Standard Recommendations) is a set of recommendations aimed at standardizing style and coding practices in the PHP ecosystem. Each standard defined within PSR affects different aspects of application development, from code organization to more complex issues like HTTP request management or using design patterns. PSR is crucial because it ensures consistency and predictability across different PHP projects and libraries.

What are PSR Standards?

PSR standards are recommendations developed by the PHP Framework Interop Group (PHP-FIG) to promote interoperability between PHP frameworks and libraries. These standards cover various aspects of PHP development, including coding standards, autoloading, HTTP message interfaces, and more.

PSR-1: Basic Coding Standard

PSR-1 establishes basic coding standards for PHP code. It covers fundamental aspects like PHP tags, character encoding, and namespace/class naming conventions. This standard ensures that PHP code follows consistent basic formatting rules.

PSR-4: Autoloading Standard

PSR-4 defines a specification for autoloading classes from file paths. It describes how to map namespace names to directory structures, making it easier to organize and load PHP classes automatically. This standard has largely replaced PSR-0 and is widely adopted in modern PHP applications.

// PSR-4 Example
namespace Vendor\Package\SubNamespace;

class ClassName
{
    // Class implementation
}

PSR-7: HTTP Message Interface

PSR-7 describes common interfaces for representing HTTP messages as defined in RFC 7230 and RFC 7231. It provides a consistent way to handle HTTP requests and responses across different PHP frameworks and libraries.

PSR-11: Container Interface

PSR-11 standardizes container interfaces for dependency injection. It defines how containers should behave and what methods they should implement, making it easier to swap different DI containers in applications.

PSR-12: Extended Coding Style

PSR-12 extends PSR-1 and replaces PSR-2. It provides more comprehensive coding style guidelines, including formatting rules for modern PHP features like anonymous classes, closures, and more recent syntax additions.

Benefits of Following PSR Standards

  • Improved code consistency across projects
  • Better interoperability between libraries
  • Easier team collaboration
  • Reduced learning curve for new projects
  • Better tool support (IDEs, static analyzers)

Want to see more practical examples? Download free ebook sample 📥 or Buy PHP course 🎓