4. Project Structure

Directory Structure:

  • Organize files into logical directories.
  • Use subdirectories for different components (e.g., controllers, models, views).
  • Keep the root directory clean.
  • Use version control (e.g., Git) to manage changes.
  • Use a .gitignore file to exclude unnecessary files.
  • Whenever possible use composer to manage dependencies.
  • Whenever possible separate assets (js, css) into separate files. Avoid using a single file for everything.

Example structure:

  • project/
    • app/
      • Controllers/
      • Models/
      • Views/
    • public/
      • assets/
        • js/
        • css/
        • images/
      • index.php
    • views/
    • config/
    • core/
    • tests/
      • Unit/
      • Integration/
    • vendor/

Namespaces:

Use namespaces to organize code and avoid naming collisions.

// Good
namespace App\Controllers;  

use App\Models\User;  

// Bad
namespace App\Controllers;  

include_once '../../Models/User.php'; 

File Naming:

// Good: Any class should consider the name convention below
UserManager.php  

// Bad 
user_manager.php 

// Good: For the views, should follow the convention below
user-manager.php
// or
user-manager.view.php

// Bad
user_manger.php