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-lazy-loading.md

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

Lazy Loading

Load resources and perform expensive operations only when they are actually needed, not at construction time.

Bad Example

<?php

declare(strict_types=1);

class ReportService
{
    private array $allUsers;
    private array $allOrders;
    private PDFGenerator $pdf;

    public function __construct(
        private readonly UserRepository $userRepo,
        private readonly OrderRepository $orderRepo,
    ) {
        // Loads everything upfront even if not all methods are called
        $this->allUsers = $this->userRepo->findAll();
        $this->allOrders = $this->orderRepo->findAll();
        $this->pdf = new PDFGenerator(); // Expensive initialization
    }

    public function getUserCount(): int
    {
        return count($this->allUsers);
    }

    public function generateReport(): string
    {
        return $this->pdf->generate($this->allOrders);
    }
}

// Just calling getUserCount() loads ALL orders and initializes PDF engine
$report = new ReportService($userRepo, $orderRepo);
echo $report->getUserCount();

Good Example

<?php

declare(strict_types=1);

class ReportService
{
    private ?array $users = null;
    private ?PDFGenerator $pdf = null;

    public function __construct(
        private readonly UserRepository $userRepo,
        private readonly OrderRepository $orderRepo,
    ) {}

    public function getUserCount(): int
    {
        return $this->userRepo->count(); // Query only what's needed
    }

    public function generateReport(): string
    {
        $orders = $this->orderRepo->findAll(); // Load when needed
        return $this->getPdf()->generate($orders);
    }

    private function getPdf(): PDFGenerator
    {
        // Lazy initialization - created only on first use
        return $this->pdf ??= new PDFGenerator();
    }

    /** @return array<User> */
    private function getUsers(): array
    {
        return $this->users ??= $this->userRepo->findAll();
    }
}

// getUserCount() only runs a COUNT query - no data loaded
$report = new ReportService($userRepo, $orderRepo);
echo $report->getUserCount();

Why

  • Faster Startup: Constructor does minimal work
  • Less Memory: Only loads data that's actually used
  • Null Coalescing Assignment: ??= provides clean lazy init pattern
  • Query Optimization: Use COUNT queries instead of loading all records
  • Pay for What You Use: Expensive resources created only when needed

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    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.

  • Socket17d

    No alerts

  • Snyk17d

    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