⚡ AutoSnippets PRO PL

PHP: Production Code Snippets

Production PHP patterns: routing, PDO, RBAC, job queues, digital signatures.

Python JavaScript TypeScript Java C# C++ Go PHP Rust SQL

On this page:

36. PHP: Modern HTTP Router (OOP)

PHP

Matching URLs to controllers with parameters.

class Router {
    private array $routes = [];

    public function add(string $method, string $path, callable $handler): void {
        $this->routes[$method][$path] = $handler;
    }

    public function dispatch(string $method, string $uri): mixed {
        return $this->routes[$method][$uri]() ?? http_response_code(404);
    }
}

When to use

The foundation of a custom microframework or a simple API without depending on Laravel or Symfony.

Common pitfalls

This simple router doesn't support path parameters (e.g. /users/{id}) — that requires additional pattern-matching logic.

37. PHP: PDO with Transactions

PHP

Prepared statements resistant to SQL Injection.

function transferFunds(PDO $pdo, int $from, int $to, float $amount): void {
    try {
        $pdo->beginTransaction();
        // Operacje SQL...
        $pdo->commit();
    } catch (Exception $e) {
        $pdo->rollBack();
        throw $e;
    }
}

When to use

Essential for operations that must succeed together or not at all — e.g. transferring funds between two accounts.

Common pitfalls

By default PDO doesn't throw exceptions on SQL errors unless you set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION — without it, rollback may never run.

38. PHP: RBAC System (Role-Based Access)

PHP

Checking user permissions against resources.

class RBAC {
    private array $roles = ['admin' => ['delete', 'write', 'read'], 'user' => ['read']];

    public function hasPermission(string $role, string $permission): bool {
        return in_array($permission, $this->roles[$role] ?? []);
    }
}

When to use

A basic access-control mechanism — checking whether a user's role is allowed to perform a given action.

Common pitfalls

Hard-coding roles and permissions makes them harder to manage in a larger app — eventually move them into a database.

39. PHP: Background Job Queue (CLI)

PHP

A parser and dispatcher for async tasks, driven from the console.

class TaskDispatcher {
    public function dispatch(string $jobClass, array $data): void {
        $payload = json_encode(['job' => $jobClass, 'data' => $data]);
        file_put_contents(__DIR__ . '/queue.log', $payload . PHP_EOL, FILE_APPEND);
    }
}

When to use

Useful for deferring time-consuming operations (sending emails, generating reports) outside the main HTTP request.

Common pitfalls

Writing to a file as a queue doesn't scale with many concurrent processes — a real broker (Redis, RabbitMQ) works better in production.

40. PHP: OpenSSL Digital Signatures

PHP

Cryptographically securing data sent between systems.

function signData(string $data, string $privateKey): string {
    openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA256);
    return base64_encode($signature);
}

When to use

Use it when signing data sent between systems, so the recipient can verify its authenticity.

Common pitfalls

The private key used for signing must be strictly protected — if leaked, anyone could impersonate your system.