May 25, 2021 Composer
For modern languages, package managers are basically standard. J ava has Maven, Python has pip, Ruby has gem, Nodejs has npm. PHP's is PEAR, but PEAR pits are many:
Fortunately, we have Composer, PHP-dependent management weapon. It's open source, it's easy to use, and it's easy to submit your own package.
Composer requires PHP 5.3.2 plus to operate.
$ curl -sS https://getcomposer.org/installer | php
This command
composer.phar
to the current directory.
PHAR (PHP package) is a compressed format that can run directly from the command line.
You can use
--install-dir
option to install Compasser to a specified directory, for example:
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
Of course, you can also do a global installation:
$ curl -sS https://getcomposer.org/installer | php
$ mv composer.phar /usr/local/bin/composer
Homebrew can also be installed under Mac OS X:
brew tap josegonzalez/homebrew-php
brew install josegonzalez/php/composer
Typically,
composer.phar
PATH
environment variable, not necessarily a global installation.
Create a
composer.json
file in the project directory that indicates dependencies, such as your project dependency
monolog:
{
"require": {
"monolog/monolog": "1.2.*"
}
}
Installation dependencies are simple and simply run in the project directory:
composer install
If there is no global installation, run:
php composer.phar install
Composer provides the feature of autoloading by simply adding the following line to the initialization section of your code:
require 'vendor/autoload.php';
packagist.org is Comcoser's repository, where many well-known PHP libraries can be found. You can also submit your own work.
The basic usage of Compaser is described above. Composer also has some advanced features that, while not required, often facilitate PHP development.