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.

rulesmodern-override-attribute.md

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

Override Attribute

Use #[\Override] on methods that override a parent method (PHP 8.3+).

Bad Example

<?php

declare(strict_types=1);

class BaseRepository
{
    public function findById(int $id): ?object
    {
        // Base implementation
        return null;
    }

    public function save(object $entity): void
    {
        // Base implementation
    }
}

class UserRepository extends BaseRepository
{
    // Typo in method name - creates new method instead of overriding!
    public function findByld(int $id): ?User  // 'l' instead of 'I'
    {
        return User::find($id);
    }

    // Parent renames save() to persist() - this silently becomes dead code
    public function save(object $entity): void
    {
        // This method is never called if parent renames it
    }
}

Good Example

<?php

declare(strict_types=1);

class BaseRepository
{
    public function findById(int $id): ?object
    {
        return null;
    }

    public function save(object $entity): void
    {
        // Base implementation
    }
}

class UserRepository extends BaseRepository
{
    // Typo caught when class is loaded!
    // #[\Override]
    // public function findByld(int $id): ?User  // Fatal error: method does not override

    #[\Override]
    public function findById(int $id): ?User
    {
        return User::find($id);
    }

    #[\Override]
    public function save(object $entity): void
    {
        // If parent renames save(), this throws a fatal error
        $entity->save();
    }
}

// Works with interfaces too
interface Renderable
{
    public function render(): string;
}

class Button implements Renderable
{
    #[\Override]
    public function render(): string
    {
        return '<button>Click</button>';
    }
}

// Works with abstract classes
abstract class Controller
{
    abstract protected function authorize(): bool;
}

class UserController extends Controller
{
    #[\Override]
    protected function authorize(): bool
    {
        return auth()->check();
    }
}

Why

  • Catches Typos: Misspelled method names are caught when the class is loaded
  • Refactoring Safety: Renaming or removing a parent method triggers an error
  • Clear Intent: Signals that a method is meant to override a parent
  • Interface Changes: Detects when an interface removes a method
  • Low Overhead: Validated at class loading, no per-call cost

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