OnlineCBT Logo OnlineCBT
PHP · LESSON 19

Composer and PSR-4 Autoloading

By Ratan Updated: 16 Oct 2026

Manage PHP third-party packages using Composer and configure PSR-4 namespace autoloading.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. What is Composer?

Composer is the official dependency manager for PHP. It manages external libraries, packages, and frameworks required by your project, ensuring matching version constraints and automated dependency resolution.

2. The composer.json File

Every Composer-driven project features a composer.json file at its root specifying metadata and required packages:

{
  "name": "cbt/portal-backend",
  "description": "Online CBT Examination Platform API",
  "require": {
    "php": "^8.1",
    "guzzlehttp/guzzle": "^7.8",
    "vlucas/phpdotenv": "^5.6"
  },
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }
}

3. PSR-4 Autoloading Standard

Before PSR-4, PHP developers had to manually write dozens of require_once lines at the start of every script. The PSR-4 standard maps PHP namespaces directly to directory paths.

For example, if App\ maps to src/, then:

  • App\Controllers\AuthController resolves automatically to src/Controllers/AuthController.php.
  • App\Models\User resolves automatically to src/Models/User.php.

4. Using Autoloading in Your Code

After running composer install or composer dump-autoload, include the single autoloader file at your application entry point:

load();

// Instantiate custom controller without manual require
$auth = new AuthController();
$auth->handleLogin();

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat