开发规范
# vue
| 对象 | 命名规范 | 示例 |
|---|---|---|
| 项目名称 | kebab-case | my-vue-project |
| 目录名称 | kebab-case / 复数形式 | components/、composables/ |
| 组件文件(SFC) | PascalCase 或 kebab-case(统一) | UserCard.vue 或 user-card.vue |
| 组件 name 属性 | PascalCase | 'UserCard' |
| 基础组件前缀 | Base/App/V + PascalCase | BaseButton.vue |
| 单例组件前缀 | The + PascalCase | TheHeader.vue |
| 页面文件 | PascalCase | HomePage.vue |
| 变量/函数 | camelCase | userName、fetchData |
| 常量 | UPPER_SNAKE_CASE | MAX_RETRY_COUNT |
| 布尔变量 | is/has/can + camelCase | isLoading |
| 组合式函数 | use + camelCase | useAuth |
| 接口/类型 | PascalCase | UserProfile |
| Props 接口 | PascalCase + Props 后缀 | UserCardProps |
| 枚举名称 | PascalCase | UserStatus |
| 枚举成员 | UPPER_SNAKE_CASE | ACTIVE |
| 路由名称 | camelCase | userProfile |
| 路由路径 | kebab-case | /user-profile |
| Props 声明(JS) | camelCase | greetingText |
| Props 模板使用 | kebab-case | greeting-text |
| 事件名 | kebab-case | @update-value |
| 事件处理函数 | camelCase | handleUpdate |
# php
| 类别 | 命名规范 | 示例 | 备注 |
|---|---|---|---|
| 项目根目录 | kebab-case | my-php-app、ecommerce-api | 建议小写+短横线 |
| 普通文件 | PascalCase(类文件) | ||
| kebab-case(配置文件/入口) | UserController.php | ||
| config/app.php、public/index.php | 类文件必须与类名一致 | ||
| 目录名 | PascalCase(命名空间对应) | ||
| 或 kebab-case(资源目录) | src/Service/ | ||
| templates/、public/ | 与命名空间结构匹配 | ||
| 命名空间 | PascalCase,与目录结构对应 | namespace App\Service; | 顶层通常为 App |
| 类名 | PascalCase | class UserProfile { } | |
| class PaymentServiceProvider { } | 名词或名词短语 | ||
| 接口名 | PascalCase,可用 Interface 后缀(非强制) | interface LoggerInterface { } | |
| interface Cacheable { } | 框架推荐后缀 | ||
| Trait 名 | PascalCase,常用 Trait 后缀 | trait SingletonTrait { } | |
| trait Loggable { } | 描述复用行为 | ||
| 抽象类名 | PascalCase,常用 Abstract 前缀 | abstract class AbstractController { } | |
| abstract class BaseModel { } | 突出抽象性 | ||
| 方法名 | camelCase | getUser()、handleRequest()、isActive() | 动词开头(布尔返回用 is/has/can 前缀) |
| 普通变量 | camelCase | $userName、$totalPrice、$isLoggedIn | 局部变量 |
| 全局变量 | 避免使用,若必须用 $_ + camelCase | $_appConfig | 极不推荐 |
| 常量 | UPPER_SNAKE_CASE | const MAX_LIMIT = 100; | |
| define('DB_PASSWORD', 'secret'); | 类常量同样规则 | ||
| 类属性 | camelCase(非公有可用 _ 前缀,但不推荐) | public $firstName; | |
| protected $userAge; | PSR-12 不强制下划线前缀 | ||
| 私有属性 | camelCase,可用 _ 前缀(视团队规范) | private $_db; 或 private $db; | 多数现代 PHP 代码已弃用下划线 |
| 函数/闭包 | snake_case(传统)或 camelCase(现代) | function format_date() { } 或 function formatDate() { } | PSR-1 不 |
| 文件名(不含类) | snake_case 或 kebab-case | autoload.php、helper-functions.php | 非类文件 |
| 视图文件 | snake_case 或 kebab-case | user_profile.php、order-list.php | 模板文件 |
| 数据库表名 | snake_case,复数形式 | users、product_categories | 常见约定(如 Laravel) |
| 数据库字段 | snake_case | user_id、created_at、is_active | 统一小写+下划线 |
| JSON/API 字段 | snake_case 或 camelCase(按团队) | user_name 或 userName | 推荐与数据库一致 |
| 枚举类(PHP 8.1+) | PascalCase,成员 UPPER_SNAKE_CASE | enum UserStatus: string { case ACTIVE = 'active'; } | 成员不加 |
| 命名空间下的子目录 | PascalCase | App\Http\Controller | 与类名风格统一 |
| 测试类 | 类名以 Test 结尾,方法 test 前缀 | class UserServiceTest { function testGetUser() } | PHPUnit 约定 |
| 魔术方法 | 固定小写蛇形 | __construct()、__invoke()、__toString() | 不可自定义 |
| 资源库/服务类 | 名词 + Repository/Service 后缀 | UserRepository、MailService | 明确职责 |
| 控制器方法 | 动作动词 + 资源(camelCase) | store()、update()、destroy() | 常见 RESTful 风格 |
| 中间件 | PascalCase + Middleware 后缀 | AuthMiddleware、CorsMiddleware | 框架约定 |
| 异常类 | PascalCase + Exception 后缀 | ValidationException、FileNotFoundException | 必须继承 Exception |
上次更新: 2026/04/03, 13:57:55