Skip to content
Learn Netverks

Lesson

Step 33/36 92% through track

composer-autoload

Composer and autoloading

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Composer and autoloading: the syntax, APIs, and habits you need before advancing in PHP.

Composer autoloading is how real PHP projects organize code—without it, namespaces feel academic.

You will apply Composer and autoloading in contexts like: PSR-4 autoloaded apps, Laravel packages, and shared internal libraries.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

Toward the end of the track—consolidate before capstone-style review lessons.

Composer is PHP's dependency manager. It installs packages from Packagist and generates an autoloader so you use App\Models\User; instead of manual require chains.

composer.json essentials

{
  "require": {
    "monolog/monolog": "^3.0"
  },
  "autoload": {
    "psr-4": {
      "App\\": "src/"
    }
  }
}

Workflow

  1. composer install — install from lock file
  2. composer require vendor/package — add dependency
  3. require 'vendor/autoload.php'; in front controller

PSR-4 autoloading

Namespace App\Controllers\UserController maps to src/Controllers/UserController.php. Frameworks (Laravel, Symfony) build on this convention.

Important interview questions and answers

  1. Q: composer.json vs composer.lock?
    A: JSON declares ranges; lock pins exact versions for reproducible installs—commit lock in apps.
  2. Q: PSR-4?
    A: Autoloading standard mapping namespaces to directory paths.

Self-check

  1. Which file do you require to enable autoloading?
  2. Why commit composer.lock?

Tip: Commit composer.lock in applications so deploys install identical dependency versions.

Interview prep

PSR-4 autoloading?

Maps namespace prefixes to directory paths so use App\Model\User loads the correct file without manual requires.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • PSR-4 mapping?
  • vendor commit policy?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump