PHP Package Management: Adding a Package to Your Program

The require command adds a package to your program. At a minimum, require must be told the name of the package to add. Example 16-1 adds the Swift Mailer library to your program.

Example 16-1. Adding a package with require

php composer.phar require swiftmailer/swiftmailer

This command downloads the package, installs its files under a directory named ven­dor in your current project directory, and updates the composer.json file. The composer.json file tracks the packages you’ve installed as well as other Composer-managed settings about your project. Composer also maintains a composer.lock file, which tracks the specific versions of packages you’ve installed.

Once Composer has installed a package, all you have to do to make it available to your program is to reference the Composer autoload file with this simple line of PHP code: require “vendor/autotoad.php;”. The logic in this file contains a mapping from class names to filenames. When you reference a class in an installed package, it ensures that the files that define the class are loaded.

You only need to load the vendor/autoload.php file once, no matter how many pack­ages you have installed. Programs that rely on Composer-installed packages generally make the require “vendor/autoload.php“; statement one of the first things in the program. Example 16-2 shows that statement in the context of using Swift Mailer to create a message (as discussed in Chapter 17).

Example 16-2. Using a Composer-installed library

// Tell PHP to load Composer’s class-finding logic

require ‘vendor/autoload.php’;

// The Swift_Message class is now automatically available

$message = Swift_Message::newInstance();


$message->setTo(array(j’ames@exampte.colV => ‘James Beard’));

$message->setSubject(‘Deticious New Recipe’);


Dear James,

You should try this: puree 1 pound of chicken with two pounds of asparagus in the btender, then drop smatt batts of the mixture into a deep fryer. Yummy!





If your program is being checked into a source control system (see Chapter 14), you need to take a few steps to make sure things play nicely with Composer. First, make sure that you include both composer.json and composer.lock in the files that are tracked by the source control system. These are necessary for somebody else who checks out the program from the source control system to be able to install the same packages and the same package versions as you have. Second, make sure that the ven­dor directory is not tracked by the source control system. All the code in vendor is managed by Composer. When you upgrade a package version, the only files you want to track changes in are the composer.json and composer.lock files—not all of the indi­vidual files under vendor that may have changed.

With composer.json and composer.lock but not vendor in source control, another per­son who checks out your code from the source control system just has to run the command php composer.phar install and they will have all of the right versions of the right packages in the right place.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *