DEV Community

Russell Jones
Russell Jones

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

PSR-1: Basic Coding Standard in PHP

Updated on Jan 7, 2025: Improved writing style and examples for better clarity.
Updated on Jan 10, 2025: Removed emojis and simplified language for consistency.
Updated on Feb 16, 2025: Added additional comments and clarifications.

Ahnii!

Prerequisites: Basic PHP syntax, a code editor. No prior PSR knowledge needed — this is where you start!

Have you ever pulled down a PHP project and felt like you were reading five different coding styles at once? That's a common frustration when working with legacy codebases. PSR-1 can save you from this headache.

Understanding PSR-1

Think of PSR-1 as the "house rules" for PHP code. Just like how every house has basic rules (shoes off at the door, close the fridge, turn off lights), PSR-1 sets the foundation for writing clean PHP code that everyone can understand.

Files and Namespaces

Here are the ground rules:

  • Only use <?php and <?= tags (forget about those old-school short tags).
  • Always use UTF-8 without BOM (it prevents weird encoding issues).
  • Keep your files focused - either declare stuff OR do stuff, not both.

Naming Things Right

Let's make it clear:

  • Classes use StudlyCaps (like UserManager, OrderProcessor).
  • Constants should be in UPPER_CASE (like MAX_ATTEMPTS, API_VERSION).
  • Methods use camelCase (like getUserById, processOrder).

Real-World Example

Here's a practical example from the repository:

<?php

namespace JonesRussell\PhpFigGuide\PSR1;

/**
 * User management class following PSR-1 standards.
 *
 * This class provides methods to manage user-related operations, including
 * retrieving user information by ID and defining constants for versioning
 * and error types.
 */
class UserManager
{
    /**
     * Version number of the implementation.
     *
     * @var string
     */
    public const VERSION = '1.0.0';

    /**
     * Error type constant for not found errors.
     *
     * @var string
     */
    public const ERROR_TYPE_NOT_FOUND = 'not_found';

    /**
     * Get user information by ID.
     *
     * This method retrieves user data based on the provided user ID.
     * It returns an associative array containing the user's ID and name.
     *
     * @param  int $id The user ID to retrieve.
     * @return array User data with 'id' and 'name' keys.
     */
    public function getUserById(int $id): array
    {
        // Implementation
        return ['id' => $id, 'name' => 'John Doe'];
    }
}
Enter fullscreen mode Exit fullscreen mode

Let's break down what makes this code PSR-1 compliant:

  • Proper namespace using StudlyCaps.
  • Class name in StudlyCaps.
  • Constants in UPPERCASE_WITH_UNDERSCORES.
  • Method in camelCase.

Common Mistakes and Fixes

The Kitchen Sink File

<?php
// Don't do this - mixing declarations and side effects
echo "Hello World";
class Foo {}

// Do this instead - separate files
// config.php
echo "Hello World";

// Foo.php
class Foo {}
Enter fullscreen mode Exit fullscreen mode

Name Things Right

<?php
// Incorrect
class user_manager {}

// Correct
class UserManager {}
Enter fullscreen mode Exit fullscreen mode

Tools to Help You

These tools are useful in any project:

  • PHP_CodeSniffer: composer check-style to spot issues.
  • Auto-fixing: composer fix-style to fix common mistakes.
  • IDE Integration: Let your editor help you stay compliant.
  • Git hooks: Catch issues before they hit your repo.

Next Steps

Tomorrow, you'll explore PSR-3 and see how it makes logging consistent across your applications. This post is part of the PSR Standards in PHP series.

Resources

For more information:

Baamaapii

Top comments (0)