1 条规则
You are an expert in PHP and related web development technologies.
Core PHP Principles
- Provide precise, technical PHP examples that follow modern PHP standards.
- Emphasize object-oriented programming (OOP) for better modularity and testability.
- Focus on code reusability through iteration and modularization, avoiding duplication.
- Use descriptive and meaningful function, variable, class, and file names.
- Use lowercase with hyphens for directory names (e.g., `src/helpers`, `lib/utils`).
- Keep class files limited to one class per file and use PSR-4 autoloading.
- Add clear, descriptive comments and PHPDoc blocks to improve code clarity and maintainability.
PHP Coding Practices
- Always enable strict typing using `declare(strict_types=1);` at the top of PHP files.
- Use type hints for function arguments and return types wherever possible.
- Use typed properties to enforce consistency and clarity (PHP 7.4+).
- Use arrow functions (`fn() =>`) for concise anonymous functions where appropriate (PHP 7.4+).
- Prefer short ternary and null coalescing operators for clean fallback logic:
- Example: `$value = $input ?? 'default';`
- Avoid deep nesting; limit logic blocks to a maximum of 3 nested levels.
- Use visibility keywords (`public`, `protected`, `private`) for all properties and methods.
- Avoid public properties; use getter/setter methods to enforce encapsulation.
- Use `final` and `abstract` for classes and methods where appropriate to prevent misuse.
- Favor composition over inheritance where possible.
- Follow PSR-12 coding standard for consistent syntax and formatting.
- Avoid side effects in constructors; initialize only internal state.
- Prefer exceptions over error codes; handle errors using `try-catch` blocks.
- Do not use `@` to suppress errors; always handle them explicitly.
- Do not use global variables or functions unless absolutely necessary.
- Avoid magic numbers and strings — define constants or enums for reusability and clarity.
- Avoid circular dependencies and tightly coupled classes.
- Use consistent naming conventions: `camelCase` for variables/methods, `PascalCase` for classes.
- Use namespaces and autoloading via Composer to organize code.
- Avoid using `require`/`include` directly; rely on autoloading.
- Use `spl_autoload_register()` or Composer's autoloader for class loading.
- Use `readonly` and constructor property promotion (in PHP 8+ when upgrading).
Dependency & Project Structure Best Practices
- Use Composer for dependency management and PSR-4 autoloading.
- Organize code into `src/`, `tests/`, `vendor/`, etc., following common project structure.
- Use `.env` or a configuration class for managing environment-based settings.
- Separate business logic from presentation — avoid inline logic in views/templates.
- Write modular, testable classes with clear responsibilities.
- Use service containers or dependency injection for managing dependencies.
Error Handling and Validation
- Handle exceptions with `try-catch` and throw meaningful exceptions with context.
- Define and use custom exception classes for better error classification.
- Log errors using PSR-3 compatible loggers (e.g., Monolog).
- Never expose raw error messages to end users — use generic output and log detailed errors.
- Validate all external inputs and fail fast on unexpected values.
- Catch only exceptions you can recover from — avoid blanket `catch (\Throwable $e)` unless necessary.相关插件
Laravel↓ 7.7k
Laravel 在 Cursor 中的规则与最佳实践。Wordpress↓ 839
Wordpress 在 Cursor 中的规则与最佳实践。Expressjs↓ 88
Expressjs 在 Cursor 中的规则与最佳实践。Drupal↓ 59
Drupal 在 Cursor 中的规则与最佳实践。symfony-api↓ 41
Symfony / PHP 8+ REST API 规则与 Skill:PSR-12、严格类型、瘦控制器、Service/DTO 分层,Doctrine ORM 与校验最佳实践。Django 5↓ 13
面向 Cursor 的 Django 5.x 规则、DRF 模式、脚手架 skill 与反模式检测,可避免 N+1 查询、教授地道的 Django 写法,并覆盖 LLM 常忽略的特性。AppDeploy↓ 11
将 AI 对话中描述的应用创意转化为可运行的全栈 Web 应用。Blink↓ 0
直接在编辑器中构建并托管全栈应用,含托管数据库、认证、存储、后端、队列与自定义域名。