Professional PHP ToolkitExtra· 40 min read

Composer, Packagist & Autoloading

The tool every real PHP project uses to install packages and load classes automatically — Composer, the Packagist library, and autoloading.

What you will learn

  • Understand what Composer and Packagist are
  • Install a package and use it
  • Let Composer autoload your classes so you never write include again

The problem Composer solves

So far, every time you wanted code from another file you wrote include or require by hand. On a real project that is dozens of files, and you would also need to find, download and update outside libraries (for email, PDFs, payments) manually. Composer automates all of this. It is to PHP what npm is to JavaScript — the universal dependency manager. No modern PHP project, including Laravel, works without it.

Two words to know. A dependency is outside code your project relies on (a library someone else wrote). Packagist is the giant public catalogue of PHP packages that Composer downloads from — like an app store for code.

How a Composer project works

Composer revolves around one file, composer.json, which lists what your project needs. The flow is always the same:

  1. You install Composer once on your machine (from getcomposer.org).
  2. In your project folder you run composer require vendor/package to add a library.
  3. Composer downloads it into a vendor/ folder and records it in composer.json.
  4. You add one linerequire "vendor/autoload.php" — to your app, and every installed package (and your own classes) becomes available automatically.
  5. Anyone else can run composer install to recreate the exact same vendor/ folder from your composer.json.

Here is installing a real, popular package — a fast unique-ID generator — from the terminal:

Terminal: install a package from Packagist
# in your project folder
composer require ramsey/uuid
# Composer downloads it into vendor/ and updates composer.json

Note: Output (in the terminal): Using version ^4.7 for ramsey/uuid ./composer.json has been updated Generating autoload files Composer fetched the library and everything it needs, wrote it into vendor/, and regenerated the autoloader. You did not download or unzip anything by hand.

Now you use that package in your code. The single require of Composer’s autoloader makes the library’s classes available with no per-file includes:

Use an installed package via the autoloader
<?php
  require "vendor/autoload.php";   // the one include you still need

  use Ramsey\Uuid\Uuid;

  $id = Uuid::uuid4();
  echo "New ID: " . $id;
?>

require "vendor/autoload.php" loads Composer’s autoloader — the one and only include in a Composer project. The use line brings the Uuid class into scope (you will learn use properly in the next lesson). Uuid::uuid4() then generates a random unique identifier — code you never had to write or download manually.

Note: Output (in the browser): New ID: 1ec9b2e1-7f3a-4c2d-9b8a-2f5e1d3c4a6b (The exact ID is random each time.) Notice you never wrote include "Uuid.php" — autoloading found the class for you the moment you used it. That is the magic.

Autoloading your OWN classes

Autoloading means PHP loads a class file automatically the first time you use the class — no manual require. You tell Composer where your classes live by adding an autoload section to composer.json, then run composer dump-autoload.

composer.json — map the App namespace to the src/ folder
{
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }
}

This says "any class whose name starts with App\ lives in the src/ folder". So a class App\Models\User is expected at src/Models/User.php. After adding this and running composer dump-autoload, you can use new App\Models\User() anywhere and Composer finds and loads the file for you — no require at all.

Note: There is no page output here — this is configuration. The payoff is huge: across a 200-file project you write zero include statements. This psr-4 rule is the standard you will see in every modern PHP and Laravel project; the next lesson explains the namespaces it relies on.

Q. What is Composer used for in PHP?

Answer: Composer is PHP’s dependency manager — it installs packages from Packagist and autoloads your classes, removing manual include/require.

✍️ Practice

  1. Install Composer and run composer require ramsey/uuid in a test folder.
  2. Add the autoloader line and generate a UUID on a page.

🏠 Homework

  1. Set up a project with a psr-4 autoload mapping App\ to src/, add one class, and use it without any require.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →