Composer as a Dependency Manager
Learn to install and use open-source libraries with Composer.
The primary function of Composer is dependency management—downloading and including packages in our project. Usually, this means using open-source libraries. Using libraries is a good thing because it leads to feature-rich, well-tested code we don’t have to write. Instead, we can focus on writing our project’s business logic—the one that brings profit.
Composer downloads packages from the Composer repositories. We can configure the repository list in the composer.json
file, but usually, we don’t have to do anything unless we have set up a private Composer repository for distributing proprietary libraries. By default, Composer looks into a single repository, packagist.org. It contains most, if not all, of the open source PHP packages.
Adding a dependency
Let’s install a library named malios/php-to-ascii-table
. It provides a class that, given an array of data, can generate it as an ASCII table that looks nice in the terminal.
To add this dependency to the project, run the following command:
php composer.phar require malios/php-to-ascii-table
Get hands-on with 1200+ tech skills courses.