PHP Coding Standards (PSR)

beginner
10 min

PHP Coding Standards (PSR)

Welcome to our comprehensive guide on PHP Coding Standards (PSR). In this lesson, we'll explore the world of PSR, focusing on why these standards are essential and how they can help you write cleaner, more maintainable, and easier-to-understand PHP code.

Let's dive in! 🎯

What are PHP Coding Standards (PSR)?

PSR, or PHP Framework Interop Group Standards, are a set of guidelines and recommendations for writing consistent, scalable, and interoperable PHP code. These standards cover various aspects of PHP development, such as naming conventions, coding style, and autoloading, among others.

Why are PSRs Important?

Adhering to PSRs has several benefits:

  1. Consistency: By following a set of standards, your code becomes more consistent, making it easier for others (and yourself) to understand and maintain.
  2. Reusability: PSR-compliant code is more likely to be compatible with other PHP projects, making it easier to integrate third-party libraries and reuse existing code.
  3. Readability: PSR guidelines help ensure that your code is well-structured and easy to read, making it a breeze for new team members to jump in and contribute.
  4. Quality: Following best practices and standards generally leads to higher-quality code, reducing bugs and improving performance.

Now that you understand why PSRs are essential let's explore some of the most common PSRs you'll encounter in your PHP journey.

PSR-1: Basic Coding Standard

PSR-1 sets the foundation for clean, consistent PHP code by defining basic coding standards, such as indentation, line breaks, and naming conventions.

πŸ’‘ Pro Tip: Adhering to PSR-1 is an excellent starting point for new PHP developers.

Key Guidelines

  • Use 4 spaces for indentation
  • Limit each line to 80 characters
  • Place a single space after control structures (e.g., if, for)
  • Use double quotes for string concatenation
  • Use single quotes for string literals

Let's see an example of PSR-1-compliant code:

php
// PSR-1 compliant code <?php function addNumbers($a, $b) { $sum = $a + $b; return $sum; } // Usage echo addNumbers(3, 4); // Output: 7

PSR-2: Coding Style Guide

PSR-2 builds upon PSR-1 by providing a more comprehensive set of coding style guidelines. PSR-2 is designed to be auto-compatible with PHP's built-in PHPCS tool, allowing you to easily check and enforce the standards across your projects.

πŸ’‘ Pro Tip: PSR-2 is the recommended standard for PHP projects today.

Key Guidelines

  • Align declarations on a single line
  • Use lowercase for function and variable names, except for constants
  • Use camelCase for function and variable names
  • Use a single blank line between classes
  • Use a single blank line between methods
  • Use a single blank line between properties and methods

Here's an example of PSR-2-compliant code:

php
// PSR-2 compliant code <?php namespace App; class User { private $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } // Usage $user = new App\User('John Doe'); echo $user->getName(); // Output: John Doe

PSR-4: Autoloader

PSR-4 defines a simple and consistent autoloading standard for PHP, allowing you to load classes automatically without having to manually include each one.

πŸ’‘ Pro Tip: PSR-4 is an essential standard for large PHP projects.

Key Guidelines

  • Place your code in a namespace-based directory structure
  • The vendor/autoload.php file should be present
  • Classes should be loaded from the directory corresponding to their namespace
  • Autoloader should be able to load classes with or without a file extension

Here's an example of PSR-4-compliant code:

php
// PSR-4 compliant code // file: src/App/User.php namespace App; class User { // ... } // file: composer.json { "autoload": { "psr-4": { "App\\": "src/" } } }

Wrapping Up

Understanding and following PHP Coding Standards (PSR) is essential for writing high-quality, maintainable, and scalable PHP code. By adhering to these standards, you'll make your code more consistent, reusable, and easier for others to understand.

Quiz

Quick Quiz
Question 1 of 1

What does PSR stand for in the PHP context?

We hope you found this tutorial helpful. Happy coding! πŸ“