DEV Community

Cover image for PHP PSRs : PSR-1 Basic Coding Standard
Antonio Silva
Antonio Silva

Posted on

2

PHP PSRs : PSR-1 Basic Coding Standard

What are PHP PSRs?

PHP Standard Recommendation (PSR) is a PHP specification published by PHP Framework Interop Group, it serves to standardize PHP programming concepts.

PSR-1: Basic Coding Standard

The PSR-1 (Basic Coding Standard) is a PHP-FIG (PHP Framework Interop Group) standard that defines basic coding conventions to ensure interoperability between PHP projects. It sets fundamental guidelines on naming conventions, file structures, and class definitions.

1. Files MUST Use UTF-8 Encoding Without BOM
All PHP files must use UTF-8 encoding without a BOM (Byte Order Mark).

✅ Correct:

<?php
echo "Hello, world!";
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect (with BOM, which can cause issues):

<EF BB BF><?php
echo "Hello, world!";
Enter fullscreen mode Exit fullscreen mode

2. Files SHOULD Either Declare Symbols (Classes, Functions, Constants) or Cause Side Effects (Not Both)
A PHP file should either define classes, functions, or constants or cause side effects (e.g., outputting data or modifying headers) not both.

✅ Correct (Separate Files):

// File: Game.php (Only declares a class)
<?php
class Game {
    public function start() {
        echo "Game started!";
    }
}
Enter fullscreen mode Exit fullscreen mode
// File: start.php (Only causes a side effect)
<?php
require 'Game.php';
$game = new Game();
$game->start();
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect (Mixing Declarations and Side Effects in One File):

<?php
class Game {
    public function start() {
        echo "Game started!";
    }
}

$game = new Game(); // Side effect
$game->start();
Enter fullscreen mode Exit fullscreen mode

3. Namespaces and Class Names MUST Follow PSR-4 Standards

  • Class names must follow StudlyCaps (PascalCase).
  • Namespaces should also use StudlyCap

✅ Correct:

namespace Library\Games;

class ChessGame {
    public function play() {
        echo "Game started!";
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect (Using Snake_Case or lowercase):

namespace library_games; // Incorrect

class chess_game { // Incorrect
    public function play() {
        echo "Game started!";
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Class Constants MUST Be in Uppercase with Underscores
Class constants should be defined in all uppercase letters with underscores separating words.

✅ Correct:

class Game {
    const MAX_PLAYERS = 4;
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect (Lowercase or camelCase):

class Game {
    const maxPlayers = 4; // Incorrect
}
Enter fullscreen mode Exit fullscreen mode

5. Method and Function Names MUST Use camelCase
Method and function names should be in camelCase.

✅ Correct:

function getGameStatus() {
    return "Running";
}

class Game {
    public function startGame() {
        echo "Started!";
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect (Using snake_case or PascalCase for methods and functions):

function get_game_status() { // Incorrect
    return "Running";
}

class Game {
    public function StartGame() { // Incorrect
        echo "Started!";
    }
}
Enter fullscreen mode Exit fullscreen mode

Summary of PSR-1

Rule Description
UTF-8 Encoding PHP files must use UTF-8 without BOM
Separate Declarations and Side Effects A file should either declare symbols (classes, functions) or cause side effects, not both
Namespaces & Class Names Use StudlyCaps for class names and proper namespacing
Class Constants Must be UPPER_CASE_WITH_UNDERSCORES
Method & Function Names Must be in camelCase

Redis image

62% faster than every other vector database

Tired of slow, inaccurate vector search?
Redis delivers top recall and low latency, outperforming leading vector databases in recent benchmarks. With built-in ANN and easy scaling, it’s a fast, reliable choice for real-time AI apps.

Get started

Top comments (0)

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay