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-property-hooks.md

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

Property Hooks

Use property hooks to define get/set logic directly on properties (PHP 8.4+).

Bad Example

<?php

declare(strict_types=1);

// Verbose getter/setter boilerplate
class User
{
    private string $firstName;
    private string $lastName;
    private string $email;

    public function __construct(string $firstName, string $lastName, string $email)
    {
        $this->setFirstName($firstName);
        $this->setLastName($lastName);
        $this->setEmail($email);
    }

    public function getFirstName(): string
    {
        return $this->firstName;
    }

    public function setFirstName(string $value): void
    {
        $this->firstName = ucfirst(strtolower($value));
    }

    public function getLastName(): string
    {
        return $this->lastName;
    }

    public function setLastName(string $value): void
    {
        if (strlen($value) < 2) {
            throw new \InvalidArgumentException('Too short');
        }
        $this->lastName = $value;
    }

    public function getFullName(): string
    {
        return $this->firstName . ' ' . $this->lastName;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function setEmail(string $value): void
    {
        $this->email = strtolower($value);
    }
}

Good Example

<?php

declare(strict_types=1);

class User
{
    // Short hook syntax for simple transforms
    public string $firstName {
        set => ucfirst(strtolower($value));
    }

    // Full hook syntax for validation
    public string $lastName {
        set {
            if (strlen($value) < 2) {
                throw new \InvalidArgumentException('Too short');
            }
            $this->lastName = $value;
        }
    }

    // Virtual property (get-only, no stored value)
    public string $fullName {
        get => $this->firstName . ' ' . $this->lastName;
    }

    // Transform on set
    public string $email {
        set => strtolower($value);
    }

    public function __construct(
        string $firstName,
        string $lastName,
        string $email,
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
        $this->email = $email;
    }
}

$user = new User('PETER', 'Peterson', 'PETER@EXAMPLE.COM');
echo $user->firstName;  // "Peter"
echo $user->fullName;   // "Peter Peterson"
echo $user->email;      // "peter@example.com"

// Works with constructor promotion too
class Product
{
    public function __construct(
        public string $name { set => trim($value); },
        public float $price { set => max(0, $value); },
    ) {}
}

Why

  • Less Boilerplate: Eliminates separate getter/setter methods
  • Co-located Logic: Validation and transformation live with the property
  • Virtual Properties: Computed values without storing data
  • Direct Access: Use $obj->prop instead of $obj->getProp()
  • Constructor Compatible: Works with constructor property promotion

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