PHP PSRs : PSR-12 Extended Coding Style Guide

The PSR-12 (Extended Coding Style Guide) builds upon PSR-1 and provides detailed guidelines for formatting PHP code, making it more readable and maintainable. It is widely adopted by PHP frameworks and projects to ensure a consistent coding style. PSR-12 is an extension of PSR-2 and has the same goal as it, but with a more modern context and new features that follow the evolution of PHP. As PHP 7 has had many changes, the rewritten PSR-2 in a new PSR was necessary to meet all the news available in PHP 7. 1. File Structure & Encoding All PHP files must use UTF-8 encoding without a BOM. PHP files should use at the end in pure PHP files. ✅ Correct: 2. Namespace & Use Declarations The namespace declaration must be on the first line after declare(strict_types=1). Use statements must be placed after the namespace, grouped, and sorted alphabetically. ✅ Correct:

Feb 22, 2025 - 22:40
 0
PHP PSRs : PSR-12 Extended Coding Style Guide

The PSR-12 (Extended Coding Style Guide) builds upon PSR-1 and provides detailed guidelines for formatting PHP code, making it more readable and maintainable. It is widely adopted by PHP frameworks and projects to ensure a consistent coding style.
PSR-12 is an extension of PSR-2 and has the same goal as it, but with a more modern context and new features that follow the evolution of PHP. As PHP 7 has had many changes, the rewritten PSR-2 in a new PSR was necessary to meet all the news available in PHP 7.

1. File Structure & Encoding

  • All PHP files must use UTF-8 encoding without a BOM.
  • PHP files should use as the opening tag and should not use ?> at the end in pure PHP files.

✅ Correct:



declare(strict_types=1);

❌ Incorrect (Closing tag in a pure PHP file):



declare(strict_types=1);

?>

2. Namespace & Use Declarations

  • The namespace declaration must be on the first line after declare(strict_types=1).
  • Use statements must be placed after the namespace, grouped, and sorted alphabetically.

✅ Correct:



declare(strict_types=1);

namespace App\Controllers;

use App\Models\User;
use App\Services\AuthService;
use Exception;

❌ Incorrect (Unsorted use statements):



declare(strict_types=1);

namespace App\Controllers;

use Exception;
use App\Services\AuthService;
use App\Models\User;

3. Classes, Properties, and Methods

3.1 Class Declaration

  • The opening { must be on the same line as the class declaration.
  • Use one blank line before the class definition.

✅ Correct:

namespace App\Models;

class User {
    private string $name;

    public function getName(): string {
        return $this->name;
    }
}

❌ Incorrect (Braces on a new line):

namespace App\Models;

class User 
{
    private string $name;

    public function getName(): string
    {
        return $this->name;
    }
}

3.2 Properties

  • Visibility (public, private, protected) must always be declared.
  • One property per line.
  • Typed properties should be preferred.

✅ Correct:

class User {
    private string $name;
    private int $age;
}

❌ Incorrect (Missing visibility and multiple properties on one line):

class User {
    var $name, $age;
}

3.3 Methods

  • The function name must be in camelCase.
  • The opening { must be on the same line.
  • There should be one blank line before a method.

✅ Correct:

class User {
    private string $name;

    public function getName(): string {
        return $this->name;
    }
}

❌ Incorrect (Braces on a new line, no blank line before the method):

class User
{
    private string $name;
    public function getName(): string
    {
        return $this->name;
    }
}

4. Function and Method Arguments

  • No space before the opening parenthesis.
  • One space after the comma.
  • No trailing comma in the last parameter.

✅ Correct:

function registerUser(string $name, int $age): bool {
    return true;
}

❌ Incorrect (Extra spaces):

function registerUser ( string $name , int $age , ): bool {
    return true;
}

5. Control Structures (if, for, while, switch)

  • Braces are always required.
  • Spaces before and after parentheses.

✅ Correct:

if ($age >= 18) {
    echo "You are an adult.";
} elseif ($age > 0) {
    echo "You are a minor.";
} else {
    echo "Invalid age.";
}

❌ Incorrect (Missing braces and spaces):

if($age >= 18) echo "You are an adult.";
elseif($age > 0) echo "You are a minor.";
else echo "Invalid age.";

6. Switch Statements

  • case statements must be indented.
  • There must be a break or return in each case.

✅ Correct:

switch ($status) {
    case 'active':
        echo "User is active.";
        break;

    case 'inactive':
        echo "User is inactive.";
        break;

    default:
        echo "Unknown status.";
        break;
}

❌ Incorrect (Cases not indented, missing break):

switch ($status) {
case 'active':
    echo "User is active.";
case 'inactive':
    echo "User is inactive.";
default:
    echo "Unknown status.";
}

7. Arrays

  • Arrays must use the short syntax ([] instead of array()).
  • Each element in a multiline array should be on a new line.

✅ Correct (Short array syntax, proper formatting):

$users = [
    'Alice',
    'Bob',
    'Charlie',
];

❌ Incorrect (Old syntax, multiple values on one line):

$users = array('Alice', 'Bob', 'Charlie');

8. Visibility in Methods

  • Abstract, final, and static must be declared before visibility.

✅ Correct:

class Logger {

    public static function log(string $message): void {
        echo $message;
    }
}

❌ Incorrect (Wrong order of modifiers):

class Logger
{
    static public function log(string $message): void
    {
        echo $message;
    }
}

9. Blank Lines and Indentation

  • Use 4 spaces for indentation (not tabs).
  • One blank line between methods.
  • Two blank lines before a class definition.

✅ Correct:

namespace App\Models;


class User {
    private string $name;

    public function getName(): string {
        return $this->name;
    }

    public function setName(string $name): void {
        $this->name = $name;
    }
}

❌ Incorrect (No spacing between methods, tabs instead of spaces):

class User
{
    private string $name;
    public function getName(): string
    {
        return $this->name;
    }
    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

Summary of PSR-12

Rule Description
UTF-8 Encoding PHP files must use UTF-8 without BOM
Namespaces & Use Statements Must be declared in order and alphabetically sorted
Class Declarations Braces {} must be on the same line as the class definition
Properties & Methods One property per line, methods must use camelCase
Function Arguments No space before (), one space after ,
Control Structures Always use braces {} for if, else, for, while
Arrays Must use [] short syntax, multi-line elements on separate lines
Blank Lines One blank line before methods, two before classes