PHP Best Practices

PHP best practices, PSR standards, and code quality guidelines.

majiayu000 e48fb06 2 files · 2.0 KB Updated 567 repo stars

File contents

PHP Best Practices

Priority: P1 (HIGH)

Structure

src/
├── {Domain}/             # e.g., Services, Repositories
└── Helpers/              # Pure functions/Traits

Implementation Guidelines

  • PSR-12: Follow PSR-12 for coding style (indent, braces, space).
  • Autoloading: Use PSR-4 via Composer.
  • Namespacing: Map namespaces to directory structure.
  • PascalCase: Use for all class names.
  • camelCase: Use for methods and variables.
  • SNAKE_CASE: Use for class constants.
  • DRY Logic: Extract repetitive logic into traits/methods.
  • SOLID: Use interfaces for decoupling and SRP adherence.

Anti-Patterns

  • God Classes: No Monoliths: Avoid classes with multiple responsibilities.
  • Magic Numbers: No Hardcoding: Use class constants for config values.
  • Deep Nesting: No Nesting: Guard clauses over nested if/else.
  • Direct Output: No Echo: Return data; let controller handle response.

References

  • Clean Code Patterns

majiayu000/claude-skill-registry-data/tree/main/development/best-practices-hoangnguyen0403-agent-skills-standar commit e48fb06c7b

Frequently asked questions

npx skillmds add majiayu000/php-best-practices