What is a Dependency?

In software development, a "dependency" is a piece of pre-written code (a library or "package") that your project relies on to work. For example, instead of writing your own code to send emails, you might use a popular, well-tested emailing library. This library is a dependency of your project.

Managing these dependencies manually (downloading and including files) is messy and error-prone. This is the problem that Composer solves.


What is Composer?

Composer is a dependency manager for PHP. It's a command-line tool that handles two crucial tasks for you:

  1. It manages your project's libraries. You simply declare which libraries your project needs, and Composer will download them, find the correct versions, and even download the dependencies of those libraries.
  2. It provides an autoloader. We've already been using this feature! Composer generates the vendor/autoload.php file, which automatically loads both your own classes (via PSR-4) and the classes from any libraries you've installed.

Every modern PHP project, framework, and CMS (like WordPress, Laravel, Drupal) uses Composer.


A Practical Example: Installing a Package

Let's say we want a more powerful way to create URL slugs than our simple create_slug() function. We can use a popular, professional library called cocur/slugify.

Step 1: Open Your Terminal

Navigate your command-line terminal into your mvc-project folder that we created in the previous module.

Step 2: Tell Composer to Require the Package

Run the following command. This tells Composer, "My project requires the cocur/slugify package."

composer require cocur/slugify

You will see Composer connect to its repository (Packagist.org), find the library, download it into your vendor/ directory, and automatically update your composer.json and autoloader. It's magic!

Your composer.json will now look something like this:


{
    "name": "your-name/mvc-project",
    "description": "A simple PHP MVC framework.",
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Core/helpers.php"
        ]
    },
    "require": {
        "cocur/slugify": "^4.0"
    }
}

Step 3: Use the Library in Your Code

Now, you can use the installed library in any part of your project, and the autoloader will take care of including it.


<?php

// It's all loaded by this one file!
require_once 'vendor/autoload.php'; 

// Use the class from the library
use Cocur\Slugify\Slugify;

// Create an instance of the class
$slugify = new Slugify();

// Use its methods
echo $slugify->slugify('This is a test --- with weird characters!');
// Output: this-is-a-test-with-weird-characters

echo '<br>';

echo $slugify->slugify('เรียนรู้ PHP ให้เป็นมืออาชีพ!', 'th');
// Output: riyn-ru-php-hi-pen-mux-xachiph

As you can see, using a professional library is often more powerful and reliable than writing everything from scratch. Composer makes this process incredibly simple.


Your Mission

  1. Make sure you have Composer installed on your system.
  2. Open your terminal and navigate to your mvc-project directory.
  3. Run the command composer require cocur/slugify.
  4. Create a temporary test file (e.g., test-composer.php) in your project root, paste the example code from Step 3, and run it to see the result.

Understanding Composer is the first step to working like a modern PHP professional.