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-asymmetric-visibility.md

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

Asymmetric Visibility

Use asymmetric visibility to allow public reading but restrict writing (PHP 8.4+).

Bad Example

<?php

declare(strict_types=1);

// Using readonly - cannot modify even internally
readonly class Order
{
    public function __construct(
        public string $id,
        public string $status,      // Cannot update status after creation!
        public float $total,
    ) {}
}

// Using private with getters - verbose
class VerboseOrder
{
    public function __construct(
        private string $id,
        private string $status,
        private float $total,
    ) {}

    public function getId(): string { return $this->id; }
    public function getStatus(): string { return $this->status; }
    public function getTotal(): float { return $this->total; }

    public function markPaid(): void
    {
        $this->status = 'paid';
    }
}

echo $order->getStatus(); // Verbose

Good Example

<?php

declare(strict_types=1);

class Order
{
    public function __construct(
        // Publicly readable, only settable inside the class
        public private(set) string $id,
        public private(set) string $status,
        public private(set) float $total,
    ) {}

    public function markPaid(): void
    {
        $this->status = 'paid'; // OK - internal set
    }

    public function applyDiscount(float $percent): void
    {
        $this->total *= (1 - $percent / 100); // OK - internal set
    }
}

$order = new Order('ORD-001', 'pending', 99.99);
echo $order->status;      // OK - public read: "pending"
// $order->status = 'paid'; // Error! Cannot set from outside

$order->markPaid();
echo $order->status;      // "paid"

// Also works with protected(set)
class BaseModel
{
    public protected(set) string $table;
    public protected(set) array $fillable = [];
}

class User extends BaseModel
{
    public function __construct()
    {
        $this->table = 'users';           // OK - child class can set
        $this->fillable = ['name', 'email']; // OK
    }
}

$user = new User();
echo $user->table;       // OK - public read
// $user->table = 'foo'; // Error! Cannot set from outside

Why

  • Clean Public API: $order->status instead of $order->getStatus()
  • Internal Mutability: Unlike readonly, the class can still update its own properties
  • No Getter Boilerplate: Eliminates trivial getter methods
  • Granular Control: Choose private(set) or protected(set) for write access
  • Pairs with Hooks: Combine with property hooks for validated writes

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