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-avoid-globals.md

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

Avoid Global Variables

Never use global variables or the global keyword. Use dependency injection instead.

Bad Example

<?php

declare(strict_types=1);

// Global state - hidden dependency
$db = new PDO('mysql:host=localhost;dbname=app', 'root', '');
$config = ['debug' => true, 'cache_ttl' => 3600];

class UserService
{
    public function find(int $id): ?User
    {
        global $db; // Hidden dependency
        $stmt = $db->prepare('SELECT * FROM users WHERE id = ?');
        $stmt->execute([$id]);
        return $stmt->fetch();
    }

    public function isDebug(): bool
    {
        global $config; // Another hidden dependency
        return $config['debug'];
    }
}

// Problems:
// - Can't test without setting up globals
// - Can't trace where $db comes from
// - Any code can modify $db or $config at any time

Good Example

<?php

declare(strict_types=1);

class UserService
{
    public function __construct(
        private readonly PDO $db,
        private readonly Config $config,
    ) {}

    public function find(int $id): ?User
    {
        $stmt = $this->db->prepare('SELECT * FROM users WHERE id = ?');
        $stmt->execute([$id]);
        $data = $stmt->fetch(PDO::FETCH_ASSOC);

        return $data ? User::fromArray($data) : null;
    }

    public function isDebug(): bool
    {
        return $this->config->get('debug', false);
    }
}

// Dependencies are explicit, injectable, and testable
$service = new UserService($pdo, $config);

// Easy to test with mocks
$service = new UserService($mockPdo, new Config(['debug' => false]));

Why

  • Explicit Dependencies: Constructor shows exactly what a class needs
  • Testability: Dependencies can be mocked or stubbed
  • No Side Effects: No code can silently change shared state
  • Thread Safety: No shared mutable state
  • Refactoring: IDE can track all usages through typed properties

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