php-best-practices
PHP 8.5+ 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".
- License 许可
- MIT
- 最近更新
- 2026-08-24
- Stars
- ★ 2
- Forks
- 0
01 前言 Front Matter
- name
- php-best-practices
- license
- MIT
- metadata
- { "author": "php-community", "version": "2.0.0" }
- description
- PHP 8.5+ 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".
02 资源文件 38
- 参考文档AGENTS.md13.9 KB
- 参考文档metadata.json1.0 KB
- 参考文档README.md1.8 KB
- 参考文档rules/_sections.md1.8 KB
- 参考文档rules/_template.md1.0 KB
- 参考文档rules/modern-arrow-functions.md3.3 KB
- 参考文档rules/modern-attributes.md3.9 KB
- 参考文档rules/modern-constructor-promotion.md4.0 KB
- 参考文档rules/modern-enums-methods.md4.7 KB
- 参考文档rules/modern-enums.md6.6 KB
- 参考文档rules/modern-first-class-callables.md4.5 KB
- 参考文档rules/modern-match-expression.md3.7 KB
- 参考文档rules/modern-named-arguments.md3.1 KB
- 参考文档rules/modern-nullsafe-operator.md3.6 KB
- 参考文档rules/modern-readonly-classes.md3.3 KB
- 参考文档rules/modern-readonly-properties.md3.1 KB
- 参考文档rules/psr-12-coding-style.md4.7 KB
- 参考文档rules/psr-4-autoloading.md3.0 KB
- 参考文档rules/psr-file-structure.md5.0 KB
- 参考文档rules/psr-namespace-usage.md4.9 KB
- 参考文档rules/psr-naming-classes.md3.6 KB
- 参考文档rules/psr-naming-methods.md5.7 KB
- 参考文档rules/solid-dip.md7.0 KB
- 参考文档rules/solid-isp.md6.7 KB
- 参考文档rules/solid-lsp.md6.5 KB
- 参考文档rules/solid-ocp.md6.8 KB
- 参考文档rules/solid-single-responsibility.md8.7 KB
- 参考文档rules/solid-srp.md6.1 KB
- 参考文档rules/type-intersection-types.md2.7 KB
- 参考文档rules/type-mixed-avoid.md3.0 KB
- 参考文档rules/type-nullable-types.md2.5 KB
- 参考文档rules/type-parameter-types.md1.8 KB
- 参考文档rules/type-property-types.md1.9 KB
- 参考文档rules/type-return-types.md1.8 KB
- 参考文档rules/type-strict-mode.md4.6 KB
- 参考文档rules/type-strict-types.md1.3 KB
- 参考文档rules/type-union-types.md2.0 KB
- 参考文档rules/type-void-never.md2.9 KB
03 SKILL.md 正文
PHP Best Practices
Modern PHP 8.x patterns, PSR standards, type system best practices, and SOLID principles. Contains 45+ rules for writing clean, maintainable PHP code.
When to Apply
Reference these guidelines when:
- Writing or reviewing PHP code
- Implementing classes and interfaces
- Using PHP 8.x modern features
- Ensuring type safety
- Following PSR standards
- Applying design patterns
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Type System | CRITICAL | type- |
| 2 | Modern Features | CRITICAL | modern- |
| 3 | PSR Standards | HIGH | psr- |
| 4 | SOLID Principles | HIGH | solid- |
| 5 | Error Handling | HIGH | error- |
| 6 | Performance | MEDIUM | perf- |
| 7 | Security | CRITICAL | sec- |
Quick Reference
1. Type System (CRITICAL)
type-strict-mode- Declare strict typestype-return-types- Always declare return typestype-parameter-types- Type all parameterstype-property-types- Type class propertiestype-union-types- Use union types effectivelytype-intersection-types- Use intersection typestype-nullable- Handle nullable types properlytype-mixed-avoid- Avoid mixed type when possibletype-void-return- Use void for no-return methodstype-never-return- Use never for non-returning functions
2. Modern Features (CRITICAL)
modern-constructor-promotion- Use constructor property promotionmodern-readonly-properties- Use readonly for immutable datamodern-readonly-classes- Use readonly classesmodern-enums- Use enums instead of constantsmodern-attributes- Use attributes for metadatamodern-match-expression- Use match over switchmodern-named-arguments- Use named arguments for claritymodern-nullsafe-operator- Use nullsafe operator (?->)modern-arrow-functions- Use arrow functions for simple closuresmodern-first-class-callables- Use first-class callable syntax
3. PSR Standards (HIGH)
psr-4-autoloading- Follow PSR-4 autoloadingpsr-12-coding-style- Follow PSR-12 coding stylepsr-naming-conventions- Class and method namingpsr-file-structure- One class per filepsr-namespace-declaration- Proper namespace usage
4. SOLID Principles (HIGH)
solid-single-responsibility- One reason to changesolid-open-closed- Open for extension, closed for modificationsolid-liskov-substitution- Subtypes must be substitutablesolid-interface-segregation- Small, focused interfacessolid-dependency-inversion- Depend on abstractions
5. Error Handling (HIGH)
error-custom-exceptions- Create specific exceptionserror-exception-hierarchy- Proper exception inheritanceerror-try-catch-specific- Catch specific exceptionserror-finally-cleanup- Use finally for cleanuperror-never-suppress- Don't suppress errors with @
6. Performance (MEDIUM)
perf-avoid-globals- Avoid global variablesperf-lazy-loading- Load resources lazilyperf-array-functions- Use native array functionsperf-string-functions- Use native string functionsperf-generators- Use generators for large datasets
7. Security (CRITICAL)
sec-input-validation- Validate all inputsec-output-escaping- Escape output properlysec-password-hashing- Use password_hash/verifysec-sql-prepared- Use prepared statementssec-file-uploads- Validate file uploads
Essential Guidelines
For detailed examples and explanations, see the rule files:
- type-strict-mode.md - Strict types declaration
- modern-constructor-promotion.md - Constructor property promotion
- modern-enums.md - PHP 8.1+ enums with methods
- solid-single-responsibility.md - Single responsibility principle
Key Patterns (Quick Reference)
<?php
declare(strict_types=1);
// Constructor promotion + readonly
class User
{
public function __construct(
public readonly string $id,
private string $email,
) {}
}
// Enums with methods
enum Status: string
{
case Active = 'active';
case Inactive = 'inactive';
public function label(): string
{
return match($this) {
self::Active => 'Active',
self::Inactive => 'Inactive',
};
}
}
// Match expression
$result = match($status) {
'pending' => 'Waiting',
'active' => 'Running',
default => 'Unknown',
};
// Nullsafe operator
$country = $user?->getAddress()?->getCountry();
// Arrow functions
$names = array_map(fn(User $u) => $u->name, $users);
Output Format
When auditing code, output findings in this format:
file:line - [category] Description of issue
Example:
src/Services/UserService.php:15 - [type] Missing return type declaration
src/Models/Order.php:42 - [modern] Use match expression instead of switch
src/Controllers/ApiController.php:28 - [solid] Class has multiple responsibilities
How to Use
Read individual rule files for detailed explanations:
rules/modern-constructor-promotion.md
rules/type-strict-mode.md
rules/solid-single-responsibility.md
04 同仓库更多 Skills
three-best-practices
Three.js performance optimization and best practices guidelines. Use when writing, reviewing, or optimizing Three.js code. Triggers on tasks involving 3D scenes, WebGL/WebGPU rendering, geometries, materials, textures, lighting, shaders, or TSL.
javascript-best-practices
Modern JavaScript best practices and performance optimization guidelines. Use when writing, reviewing, or refactoring JavaScript code to ensure optimal performance, maintainability, and modern patterns.
css-best-practices
Guidelines for building modern, performant, and accessible CSS using MDN best practices. Use this when styling components, fixing layout shifts, or optimizing CSS performance.
frontend-dev
Full-stack frontend development combining premium UI design, cinematic animations, AI-generated media assets, persuasive copywriting, and visual art. Builds complete, visually striking web pages with real media, advanced motion, and compelling copy. Use when: building landing pages, marketing sites, product pages, dashboards, generating media assets (image/video/audio/music), writing conversion copy, creating generative art, or implementing cinematic scroll animations.
exa-rate-limits
Implement Exa rate limiting, backoff, and idempotency patterns. Use when handling rate limit errors, implementing retry logic, or optimizing API request throughput for Exa. Trigger with phrases like "exa rate limit", "exa throttling", "exa 429", "exa retry", "exa backoff".
exa-deploy-integration
Deploy Exa integrations to Vercel, Fly.io, and Cloud Run platforms. Use when deploying Exa-powered applications to production, configuring platform-specific secrets, or setting up deployment pipelines. Trigger with phrases like "deploy exa", "exa Vercel", "exa production deploy", "exa Cloud Run", "exa Fly.io".