DEV Community

Russell Jones
Russell Jones

Posted on • Originally published at jonesrussell.github.io on

PSR Standards in PHP: A Practical Guide for Developers

Are you tired of wrestling with inconsistent PHP codebases or struggling to make different packages work together? You're not alone! In this series, you'll explore how PHP-FIG's PSR standards can transform your development experience.

What is PHP-FIG?

PHP-FIG is a group of PHP project representatives working together to advance the PHP ecosystem. Their primary contribution is the PSR system, which defines coding standards and interfaces that enable better interoperability between PHP packages and frameworks.

Why PSRs Matter

PSRs solve several critical challenges in PHP development:

  • Code Consistency: Standardized coding styles make code more readable
  • Interoperability: Common interfaces allow different packages to work together seamlessly
  • Best Practices: Established patterns improve code quality and maintainability

Recommended Reading Path

New to PSRs? Follow this order — it builds knowledge progressively:

Foundation (Start Here)

  1. PSR-1: Basic Coding Standard — The "house rules" for PHP code
  2. PSR-12: Extended Coding Style — Detailed formatting rules (extends PSR-1)
  3. PSR-4: Autoloading Standard — How PHP finds your classes automatically

Core Infrastructure

  1. PSR-3: Logger Interface — Standardized logging across your application
  2. PSR-11: Container Interface — Dependency injection made interoperable
  3. PSR-14: Event Dispatcher — Decoupled communication between components

HTTP Stack (Read in Sequence)

  1. PSR-7: HTTP Message Interfaces — The standard "shape" of HTTP requests and responses
  2. PSR-17: HTTP Factories — Creating PSR-7 objects without coupling to implementations
  3. PSR-15: HTTP Handlers and Middleware — Processing HTTP requests through a middleware pipeline
  4. PSR-18: HTTP Client — Sending HTTP requests the standard way

Data and Caching

  1. PSR-6: Caching Interface — Full-featured cache pools and items
  2. PSR-16: Simple Cache — Lightweight key-value caching

Specialized

  1. PSR-13: Hypermedia Links — Self-documenting REST APIs with HATEOAS
  2. PSR-20: Clock Interface — Testable time handling

Quick Reference (by PSR Number)

PSR Topic Post
1 Basic Coding Standard Read
3 Logger Interface Read
4 Autoloading Standard Read
6 Caching Interface Read
7 HTTP Messages Read
11 Container Interface Read
12 Extended Coding Style Read
13 Hypermedia Links Read
14 Event Dispatcher Read
15 HTTP Handlers Read
16 Simple Cache Read
17 HTTP Factories Read
18 HTTP Client Read
20 Clock Read

Practical Learning

Each post includes:

  • A relatable analogy explaining what the standard solves
  • The actual PSR interface with commentary
  • A working implementation from the blog API companion project
  • Common mistakes with before/after fixes
  • Framework integration examples (Laravel, Symfony, Slim)
  • A "Try It Yourself" section with exact commands to run

Getting Started

To follow along, clone the companion repository — a blog API that uses all 14 PSRs:

git clone https://github.com/jonesrussell/php-fig-guide.git
cd php-fig-guide
composer install
Enter fullscreen mode Exit fullscreen mode

The blog API demonstrates every PSR in a real project context. Each PSR has:

  • Implementation code under src/
  • PHPUnit tests under tests/
# Run all tests
composer test

# Run tests for a specific PSR
composer test -- --filter=PSR7

# Check coding standards (PSR-1 + PSR-12)
composer check-style
Enter fullscreen mode Exit fullscreen mode

Resources

Baamaapii

Top comments (0)