All skills
asyrafhussin avatar

/php-best-practices

@e7ea05d

PHP 8.x modern patterns, PSR standards, and SOLID principles. Use when reviewing PHP code, checking type safety, auditing code quality, or ensuring PHP best practices. Triggers on "review PHP", "check PHP code", "audit PHP", or "PHP best practices".

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/php-best-practices

This session only. Nothing lands on disk.

rulesperf-generators.md

≈712 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Generators for Large Datasets

Use generators (yield) to process large datasets without loading everything into memory.

Bad Example

<?php

declare(strict_types=1);

// Loads entire file into memory - crashes on large files
function readLines(string $path): array
{
    return file($path); // 1GB file = 1GB+ memory
}

// Loads all records into memory
function getAllUsers(PDO $pdo): array
{
    $stmt = $pdo->query('SELECT * FROM users');
    return $stmt->fetchAll(); // 1M users = huge memory spike
}

// Building large array in memory
function generateRange(int $start, int $end): array
{
    $result = [];
    for ($i = $start; $i <= $end; $i++) {
        $result[] = $i;
    }
    return $result; // 10M items = 10M element array
}

foreach (getAllUsers() as $user) {
    processUser($user); // All users loaded before loop starts
}

Good Example

<?php

declare(strict_types=1);

// Reads file line by line - constant memory
function readLines(string $path): \Generator
{
    $handle = fopen($path, 'r');

    try {
        while (($line = fgets($handle)) !== false) {
            yield trim($line);
        }
    } finally {
        fclose($handle);
    }
}

// Fetches records in chunks - constant memory
function getAllUsers(PDO $pdo, int $chunkSize = 1000): \Generator
{
    $offset = 0;

    do {
        $stmt = $pdo->prepare('SELECT * FROM users LIMIT :limit OFFSET :offset');
        $stmt->bindValue(':limit', $chunkSize, PDO::PARAM_INT);
        $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
        $stmt->execute();
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

        foreach ($rows as $row) {
            yield User::fromArray($row);
        }

        $offset += $chunkSize;
    } while (count($rows) === $chunkSize);
}

// Lazy range - no array allocation
function lazyRange(int $start, int $end): \Generator
{
    for ($i = $start; $i <= $end; $i++) {
        yield $i;
    }
}

// Process one user at a time - constant memory
foreach (getAllUsers($pdo) as $user) {
    processUser($user);
}

// Chain generators with yield from
function activeUsers(PDO $pdo): \Generator
{
    foreach (getAllUsers($pdo) as $user) {
        if ($user->isActive()) {
            yield $user;
        }
    }
}

Why

  • Constant Memory: Process 1M records using the same memory as 1 record
  • Lazy Evaluation: Items computed on demand, not all upfront
  • Composable: Generators can be chained with yield from
  • File Processing: Read multi-GB files without memory issues
  • Database Batching: Fetch in chunks while presenting a simple iterator interface

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill is a comprehensive and safe resource for PHP 8.x best practices. It provides structured guidance on type safety, modern PHP syntax, PSR standards, and secure coding practices (such as prepared statements and password hashing). The agent is instructed to use standard environment detection commands (php -v, grep) to tailor its advice. No malicious patterns or security risks were identified.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    39 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at e7ea05d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
Other metadata
metadata
{
  "author": "php-community",
  "version": "2.1.0",
  "phpVersion": "8.0 - 8.5"
}

README badge

README badge for asyrafhussin/agent-skills/php-best-practices