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.

rulespsr-4-autoloading.md

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

PSR-4 Autoloading

Follow PSR-4 autoloading standard for class file organization.

Bad Example

<?php

// File: includes/classes/user_model.php
// Wrong: File name doesn't match class name
// Wrong: Using underscores instead of directories

class User_Model
{
    // ...
}

// File: lib/MyApp/Services/userService.php
// Wrong: File name case doesn't match class name

namespace MyApp\Services;

class UserService
{
    // ...
}

// Manual includes - fragile and error-prone
require_once 'includes/classes/user_model.php';
require_once 'includes/classes/order_model.php';
require_once 'lib/helpers.php';

Good Example

<?php

declare(strict_types=1);

// File: src/Domain/User/User.php
// Namespace matches directory structure

namespace App\Domain\User;

class User
{
    public function __construct(
        private UserId $id,
        private Email $email,
    ) {}
}

// File: src/Domain/User/UserId.php
namespace App\Domain\User;

readonly class UserId
{
    public function __construct(
        public string $value,
    ) {}
}

// File: src/Application/Services/UserService.php
namespace App\Application\Services;

use App\Domain\User\User;
use App\Domain\User\UserRepository;

class UserService
{
    public function __construct(
        private UserRepository $repository,
    ) {}
}

Composer Configuration

{
    "autoload": {
        "psr-4": {
            "App\\": "src/",
            "Tests\\": "tests/"
        }
    }
}

Directory Structure

project/
├── composer.json
├── src/
│   ├── Application/
│   │   ├── Commands/
│   │   │   └── CreateUserCommand.php
│   │   └── Services/
│   │       └── UserService.php
│   ├── Domain/
│   │   ├── User/
│   │   │   ├── User.php
│   │   │   ├── UserId.php
│   │   │   ├── Email.php
│   │   │   └── UserRepository.php
│   │   └── Order/
│   │       ├── Order.php
│   │       └── OrderRepository.php
│   └── Infrastructure/
│       ├── Persistence/
│       │   └── DoctrineUserRepository.php
│       └── Http/
│           └── Controllers/
│               └── UserController.php
└── tests/
    ├── Unit/
    │   └── Domain/
    │       └── User/
    │           └── UserTest.php
    └── Integration/
        └── UserServiceTest.php

Why

  • Automatic Loading: No manual require/include statements needed
  • Predictable Structure: Class location is deterministic from namespace
  • IDE Support: Enables full autocompletion and navigation
  • Composer Integration: Standard Composer autoloader works out of the box
  • Interoperability: Works with any PSR-4 compliant framework
  • Maintainability: Clear organization makes codebases easier to navigate

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