跳转到内容

“代码组织最佳实践”

正确的代码组织对于可维护性、可扩展性和团队协作至关重要。

良好-organizedXOOPS模区块应遵循以下结构:

mymodule/
├── xoops_version.php # Module metadata
├── index.php # Frontend entry point
├── admin.php # Admin entry point
├── class/
│ ├── Controller/ # Request handlers
│ ├── Handler/ # Data handlers
│ ├── Repository/ # Data access
│ ├── Entity/ # Domain objects
│ ├── Service/ # Business logic
│ ├── DTO/ # Data transfer objects
│ └── Exception/ # Custom exceptions
├── templates/ # Smarty templates
│ ├── admin/ # Admin templates
│ └── blocks/ # Block templates
├── assets/
│ ├── css/ # Stylesheets
│ ├── js/ # JavaScript
│ └── images/ # Images
├── sql/ # Database schemas
├── tests/ # Unit and integration tests
├── docs/ # Documentation
└── composer.json # Composer configuration
Classes: PascalCase (UserController, PostRepository)
Methods: camelCase (getUserById, createUser)
Properties: camelCase ($userId, $username)
Constants: UPPER_SNAKE_CASE (DEFAULT_LIMIT, MAX_USERS)
Functions: snake_case (get_user_data, validate_email)
Files: PascalCase.php (UserController.php)
  • 每个文件一个类
  • 文件名与类名匹配
  • 目录结构与命名空间层次结构匹配
  • 将相关课程放在一起
  • 跨模区块使用一致的命名
{
"autoload": {
"psr-4": {
"Xoops\\Module\\Mymodule\\": "class/"
}
}
}
<?php
class Autoloader
{
public static function register()
{
spl_autoload_register([self::class, 'autoload']);
}
public static function autoload($class)
{
$prefix = 'Xoops\\Module\\Mymodule\\';
if (strpos($class, $prefix) !== 0) {
return;
}
$relative = substr($class, strlen($prefix));
$file = __DIR__ . '/' .
str_replace('\\', '/', $relative) . '.php';
if (file_exists($file)) {
require $file;
}
}
}
?>
  • 每个班级都应该有一个改变的理由
  • 将关注点分成不同的类别
  • 保持课程的重点和凝聚力
  • 使用有意义的、描述性的名称
  • 遵循PSR-12编码标准
  • 除非明显,否则避免使用缩写
  • 使用一致的模式
  • 将相关课程分组在一起
  • 将关注点分离到子目录中
  • 保持模板和资源井井有条
  • 使用一致的文件命名
  • 为所有类使用正确的命名空间
  • 遵循PSR-4自动加载
  • 命名空间与目录结构匹配
  • 将配置集中在config目录中
  • 使用环境-based配置
  • 不要硬编码设置
<?php
class Bootstrap
{
private static $serviceContainer;
private static $initialized = false;
public static function initialize()
{
if (self::$initialized) {
return;
}
global $xoopsDB;
self::$serviceContainer = new ServiceContainer($xoopsDB);
self::$initialized = true;
}
public static function getServiceContainer()
{
if (!self::$initialized) {
self::initialize();
}
return self::$serviceContainer;
}
}
?>

另请参阅:

  • 异常管理错误-Handling
  • 测试组织的测试
  • ../Patterns/MVC-Pattern用于控制器结构

标签:#best-practices #code-organization #psr-4 #module-development