Courses/PHP/Composer Packages

    Lesson 46 โ€ข Advanced

    Composer Packages ๐Ÿ“ฆ

    Create reusable PHP libraries, configure PSR-4 autoloading, understand semantic versioning, and publish packages to Packagist.

    What You'll Learn in This Lesson

    • โ€ข Structure a Composer package with PSR-4 autoloading
    • โ€ข Write a proper composer.json with dependencies
    • โ€ข Understand semantic versioning (SemVer) and constraints
    • โ€ข Publish to Packagist and set up auto-updates
    • โ€ข Know when to commit composer.lock (apps vs libraries)

    Building a Package

    A Composer package is a directory with a composer.json file, PSR-4 autoloaded source code in src/, and tests in tests/. The namespace maps to the directory structure: Acme\Validator\Rules\Email lives at src/Rules/Email.php.

    Try It: Package Structure

    Generate composer.json and explore the directory layout

    Try it Yourself ยป
    JavaScript
    // Building & Publishing Composer Packages
    console.log("=== What is Composer? ===");
    console.log();
    console.log("  Composer is PHP's package manager (like npm for Node.js).");
    console.log("  Packagist.org is the default package registry.");
    console.log();
    console.log("  Key commands:");
    console.log("  composer init          โ€” Create a new package");
    console.log("  composer require X     โ€” Install a dependency");
    console.log("  composer install       โ€” Install from composer.lock");
    console.log(" 
    ...

    Versioning & Publishing

    Semantic versioning tells users what to expect: patch (1.0.1) for bug fixes, minor (1.1.0) for new features, major (2.0.0) for breaking changes. The caret constraint (^1.2) is most common โ€” it allows updates within the same major version.

    Try It: SemVer & Publishing

    Understand version constraints and the Packagist publishing workflow

    Try it Yourself ยป
    JavaScript
    // Semantic Versioning & Publishing
    console.log("=== Semantic Versioning (SemVer) ===");
    console.log();
    console.log("  MAJOR.MINOR.PATCH");
    console.log("    1.0.0  โ†’  Initial release");
    console.log("    1.0.1  โ†’  Bug fix (backward compatible)");
    console.log("    1.1.0  โ†’  New feature (backward compatible)");
    console.log("    2.0.0  โ†’  Breaking change (NOT backward compatible)");
    console.log();
    
    // Demonstrate version constraints
    let constraints = [
      { constraint: "^1.2.3", meaning: ">=1.2.3 and
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    No .gitignore for vendor/ โ€” Never commit the vendor/ directory to git. It's generated by composer install and can be hundreds of MB.
    โš ๏ธ
    Breaking SemVer โ€” Changing a method signature without bumping the major version breaks downstream projects. Follow SemVer strictly.
    ๐Ÿ’ก
    Pro Tip: Use composer outdated to see which dependencies have newer versions, and composer audit to check for security vulnerabilities.

    ๐Ÿ“‹ Quick Reference โ€” Composer

    CommandPurpose
    composer initCreate a new composer.json interactively
    composer requireAdd and install a dependency
    composer updateUpdate dependencies to latest allowed
    composer auditCheck for known security vulnerabilities
    PSR-4Autoloading standard: namespace โ†’ directory

    ๐ŸŽ‰ Lesson Complete!

    You can now build and publish packages! Next, learn to deploy PHP apps with Nginx, PHP-FPM, and Docker.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service