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-typed-constants.md

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

Typed Class Constants

Use typed class constants to enforce type safety on constant values (PHP 8.3+).

Bad Example

<?php

declare(strict_types=1);

// Untyped constants - no type checking
class PaymentGateway
{
    public const TIMEOUT = 30;
    public const CURRENCY = 'USD';
    public const RETRY_LIMIT = 3;
    public const ENABLED = true;
}

// Child class can accidentally change the type
class StripeGateway extends PaymentGateway
{
    public const TIMEOUT = '30'; // Changed from int to string - no error!
    public const ENABLED = 1;    // Changed from bool to int - no error!
}

interface HasVersion
{
    public const VERSION = '1.0.0';
}

class App implements HasVersion
{
    public const VERSION = 100; // Type changed silently - no error!
}

Good Example

<?php

declare(strict_types=1);

// Typed constants enforce type safety
class PaymentGateway
{
    public const int TIMEOUT = 30;
    public const string CURRENCY = 'USD';
    public const int RETRY_LIMIT = 3;
    public const bool ENABLED = true;

    // Array constants with typed declaration
    public const array SUPPORTED_CURRENCIES = ['USD', 'EUR', 'GBP'];
}

// Child class cannot change the type
class StripeGateway extends PaymentGateway
{
    public const int TIMEOUT = 60;        // OK - same type
    // public const string TIMEOUT = '60'; // TypeError!
}

// Interfaces with typed constants
interface HasVersion
{
    public const string VERSION = '1.0.0';
}

class App implements HasVersion
{
    public const string VERSION = '2.0.0'; // OK - same type
    // public const int VERSION = 2;        // TypeError!
}

// Enums with typed constants
enum Status: string
{
    public const string DEFAULT = 'pending';

    case Pending = 'pending';
    case Active = 'active';
}

Why

  • Type Safety: Constants are validated when the class is loaded
  • Inheritance Protection: Child classes cannot change constant types
  • Interface Contracts: Typed constants in interfaces enforce implementation types
  • Static Analysis: Tools like PHPStan and Psalm can validate constant usage
  • Self-Documenting: Type declaration makes intent clear

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