DEV Community

Cover image for PHP - PER Coding Style 2.0
Antonio Silva
Antonio Silva

Posted on

PHP - PER Coding Style 2.0

PER Coding Style 2.0 is a set of coding conventions primarily aimed at PHP projects. It is an evolution of the PER Coding Style 1.0, offering stricter rules and modern coding practices. The PHP Extended Rules (PER) provide guidelines that go beyond PSR (PHP Standards Recommendations) to improve code readability, maintainability, and quality.

Key Guidelines in PER Coding Style 2.0

1. Strict Typing

PER 2.0 enforces strict typing in PHP files.

✅ Correct

<?php declare(strict_types=1);

function add(int $a, int $b): int {
    return $a + $b;
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

<?php

function add($a, $b) {
    return $a + $b; // No type declarations
}
Enter fullscreen mode Exit fullscreen mode

2. Return Type Declarations

Functions and methods must have explicit return types.

✅ Correct

function getUserName(int $id): string {
    return 'User_' . $id;
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

function getUserName($id) {
    return 'User_' . $id; // No return type specified
}
Enter fullscreen mode Exit fullscreen mode

3. Class Property Visibility

All properties in a class must have a visibility modifier (private, protected, public).

✅ Correct

class User {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

class User {
    var $name; // 'var' is discouraged
}
Enter fullscreen mode Exit fullscreen mode

4. Single Responsibility Principle

Each class should have only one responsibility.

✅ Correct

class User {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

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

❌ Incorrect

class User {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

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

    public function saveToDatabase() {
        // Mixing responsibilities
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Consistent Naming Conventions

  • Class names: PascalCase
  • Method names: camelCase
  • Variable names: camelCase
  • Constants: UPPER_CASE_SNAKE

✅ Correct

class OrderProcessor {
    private int $orderId;

    public function processOrder(): void {
        // Process order
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

class order_processor { // Not PascalCase
    private int $OrderId; // Incorrect case

    public function ProcessOrder() { // Incorrect case
        // Process order
    }
}
Enter fullscreen mode Exit fullscreen mode

6. No Use of Global Variables

PER 2.0 discourages the use of global variables.

✅ Correct

class Config {
    public static function getDbHost(): string {
        return 'localhost';
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

global $dbHost;
$dbHost = 'localhost';
Enter fullscreen mode Exit fullscreen mode

7. No Use of null Defaults for Scalars

Functions should not use null as a default value for scalar types.

✅ Correct

function greet(string $name = 'Guest'): void {
    echo "Hello, $name!";
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

function greet(?string $name = null): void { // Discouraged
    echo "Hello, " . ($name ?? 'Guest') . "!";
}
Enter fullscreen mode Exit fullscreen mode

8. No Use of else When Avoidable

PER 2.0 recommends early return instead of else.

✅ Correct

function getDiscount(int $age): int {
    if ($age < 18) {
        return 10;
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

function getDiscount(int $age): int {
    if ($age < 18) {
        return 10;
    } else { // Unnecessary 'else'
        return 0;
    }
}
Enter fullscreen mode Exit fullscreen mode

9. Avoiding isset and empty

Use null coalescing (??) or array_key_exists() instead.

✅ Correct

$value = $data['key'] ?? 'default';
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

$value = isset($data['key']) ? $data['key'] : 'default';
Enter fullscreen mode Exit fullscreen mode

10. Using Enums Instead of Constants

Prefer Enums over class constants.

✅ Correct

enum OrderStatus: string {
    case Pending = 'pending';
    case Shipped = 'shipped';
}
Enter fullscreen mode Exit fullscreen mode

❌ Incorrect

class OrderStatus {
    const PENDING = 'pending';
    const SHIPPED = 'shipped';
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

PER Coding Style 2.0 refines best practices for PHP, ensuring code is strictly typed, readable, maintainable, and modern. By following these rules, developers can produce high-quality, robust PHP applications.

Top comments (2)

Collapse
 
xwero profile image
david duymelinck • Edited

Just read php-fig.org/per/coding-style/. It is boring stuff about placement and spaces.
Make a decision for the project and have a formatter that fixes that stuff for you.

Collapse
 
jean85 profile image
Alessandro Lai

Is this article written completely using an LLM? It's wrong!

PER-CS does not impose neither strict typing nor return types, those are not style rules.