Skip to Content
Course content

40: Dependency Management with Composer

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I want to show you a snippet of code that I've seen countless developers struggle with when they first move from "vanilla" PHP to professional projects. Imagine you've decided to use the popular Carbon library to handle some complex date math. You've run the install command in your terminal, and you're feeling good. But then you run this:

<?php
use Carbon\Carbon;

$date = Carbon::now()->addDays(5);
echo "Your subscription expires on: " . $date->toDateString();
?>

You hit refresh in your browser and get a screen-filling error: Fatal error: Uncaught Error: Class "Carbon\Carbon" not found. You check your folder, and the vendor directory is right there. The files exist. You might even try to fix it by manually including the file with require 'vendor/nesbot/carbon/src/Carbon.php';, only to get another error because Carbon depends on other classes that aren't loaded yet.

The missing bridge: vendor/autoload.php

The problem here isn't that the library isn't installed; it's that PHP has no idea where to find those classes when you call them. In the old days, we used to have massive lists of require and include statements at the top of every file. It was a maintenance nightmare. If you moved a file or added a new library, everything broke.

Composer solves this with an autoloader. Instead of you telling PHP where every single class is, Composer creates a map of the entire vendor directory. You only need to require one file, and Composer handles the rest on the fly.

<?php
require 'vendor/autoload.php'; // This is the magic line

use Carbon\Carbon;

$date = Carbon::now()->addDays(5);
echo "Your subscription expires on: " . $date->toDateString();
?>

Now, when you call Carbon::now(), PHP asks the Composer autoloader, "Hey, do you know where the Carbon class is?" Composer looks at its map and loads the file for you instantly. I can't stress this enough: if you don't have require 'vendor/autoload.php'; at your entry point, nothing you install via Composer will work.

Defining your needs in composer.json

When you run composer require nesbot/carbon, Composer doesn't just download files; it updates a file called composer.json. Think of this as the "manifest" for your project. It tells anyone else who downloads your code exactly which libraries are required to make the app run.

I highly recommend opening this file and looking at it. You'll see a "require" section. If you're working in a team, you don't commit the vendor folder to Git (it's too huge and redundant). Instead, you commit the composer.json. When your teammate clones the repo, they just run composer install, and Composer reads that manifest to rebuild the vendor folder on their machine.

Why the .lock file is your best friend in production

You'll also notice a composer.lock file. This often confuses people—they think it's a temporary file. It's not. It is arguably the most important file for stability.

The composer.json file usually allows for versions (e.g., ^2.0, which means "anything from 2.0 up to, but not including, 3.0"). If you only had the JSON file, you might install version 2.1 today, but your server might install 2.5 next week. If 2.5 introduced a breaking change, your site would crash in production while working perfectly on your laptop.

The composer.lock file records the exact version and commit hash of every package installed. When you run composer install, Composer checks the lock file first. This ensures that every single environment—your laptop, your colleague's Mac, and your Linux production server—is running the exact same byte-for-byte code.




📋 Practical Task

Building a File-Based Audit Log with Monolog

In this exercise, you will integrate a professional logging library into a project. Instead of using error_log() or echo to track events, you'll use Monolog, the industry standard for PHP.

Your Goal: Create a script that logs a "User Login" event to a local file called app.log.

  1. Open your terminal in your project directory and install Monolog by running: composer require monolog/monolog.
  2. Create a file named logger.php.
  3. Inside logger.php:
    • Include the Composer autoloader.
    • Import the Logger and StreamHandler classes from the Monolog namespace.
    • Create a new Logger instance named "auth_system".
    • Push a StreamHandler to the logger that points to a file named app.log and sets the minimum logging level to Logger::INFO.
    • Trigger a log entry using $log->info('User "Admin" successfully logged in.');
  4. Run the script via CLI (php logger.php) and verify that app.log was created and contains the log entry with a timestamp.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.