PHP Best Practices
Comprehensive coding standards for PHP development, optimized for AI agents and LLMs. Contains 24 rules across 8 categories, prioritized by impact.
When to Apply
Reference these guidelines when:
- Writing PHP application code
- Developing WordPress plugins or themes
- Building Laravel applications
- Implementing security measures
- Optimizing PHP performance
- Following PSR standards
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Security | CRITICAL | security- |
| 2 | Error Handling | HIGH | error- |
| 3 | Performance | HIGH | perf- |
| 4 | Type Safety | MEDIUM-HIGH | types- |
| 5 | OOP Patterns | MEDIUM | oop- |
| 6 | PSR Standards | MEDIUM | psr- |
| 7 | Testing | MEDIUM | test- |
| 8 | Modern PHP | LOW-MEDIUM | modern- |
Quick Reference
1. Security (CRITICAL)
security-input-validation- Validate with filter_var()security-output-escaping- Escape based on contextsecurity-password-hashing- Use password_hash()security-csrf-tokens- Implement CSRF protectionsecurity-no-eval- Never use eval()
2. Error Handling (HIGH)
error-exception-handling- Use try-catch properlyerror-custom-exceptions- Create domain exceptionserror-error-reporting- Configure error levelserror-logging- Use PSR-3 logging
3. Performance (HIGH)
perf-opcache-enabled- Enable OPcacheperf-autoloading- Use Composer autoloaderperf-string-interpolation- Prefer interpolationperf-generators-memory- Use generators for large data
4. Type Safety (MEDIUM-HIGH)
types-strict-types- Declare strict_types=1types-return-types- Always declare return typestypes-nullable-types- Use ?Type for nullabletypes-union-types- Use union types
5. OOP Patterns (MEDIUM)
oop-final-classes- Prefer final classesoop-interface-segregation- Small interfacesoop-dependency-injection- Inject dependencies
6. PSR Standards (MEDIUM)
psr-coding-style- Follow PSR-12psr-autoloading- Use PSR-4psr-http-messages- Use PSR-7
7. Modern PHP (LOW-MEDIUM)
modern-constructor-promotion- Use property promotion
How to Use
Read individual rule files for detailed explanations and code examples.
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md