Chapter 16. Package Management

Take the virtuous laziness introduced in Chapter 5 to a powerful new level by relying on entire packages of code that other people have written. This chapter shows you how to use the Composer package management system to find existing libraries and integrate them into your programs.

If you’ve tried to integrate third-party libraries without a package manager before, you’re probably familiar with all the steps that entails: downloading an archive file containing the library, unpacking it, putting the unpacked files in a special place, and then modifying your program so it can find the new files.

With Composer, all of that is reduced to a single command. Plus, when newer versions of the packages you use are released, Composer can upgrade them in a snap.

If you’ve used a package manager in another language (such as npm with JavaScript, gem with Ruby, or cpan with Perl) you’ll find the Composer experience familiar and pleasant.

Installing Composer

Download and run Composer’s installer by running this command at a shell prompt in your terminal:

curl -sS https://getcomposer.org/installer | php

On Windows, download and run the Composer installer and then run Composer-Setup.exe.

If you’ve installed Composer successfully, when you run it from the command line (by typing php composer.phar, or just composer on Windows, you should see a help screen listing the commands that Composer supports.

Adding a Package to Your Program

The require command ...

Get Learning PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.