Basis of Composer for PHP

Composer for PHP

Basis of Composer for PHP
Photo by Pierre Châtel-Innocenti / Unsplash

The Core File

  • composer.json

The Keys

The require Key

Specifying the dependency of the project, the packages that the project depends on.

{
    "require": {
        "monolog/monolog": "1.0.*" // "package name" : "version"
    }
}

Package Name (monolog/monolog)

Consist of a vendor name and a library name

Version (1.0.*)

Range: >, >=, <, <=, !=
Hyphen(-): 1.0 - 2.0 equals >=1.0.0 <2.1
Wildcard(*): 1.0.* equals >=1.0 <1.1
Tilde(~): ~1.2 equals >=1.2 <2.0.0; ~1.2.3 equals >=1.2.3 <1.3.0 // depends on till the next subversion
Caret(^): ^1.2.3 equals >=1.2.3 <2.0.0 // depends on till the next version

Install the Dependencies

php composer.phar install

vendor The conventional folder for all third party library

composer.lock

The file generated followed by the install command(or the update command). It consist of the exact version that the command has installed

update command

php composer.phar update

Packagist

The main Composer repository

Autoloading

vendor/autoload.php The file to autoload all dependency library

Use autoloading

require __DIR__ . '/vendor/autoload.php';

The autoload Field

Ref

Specifying your own code to the autoloader

{
    "autoload": {
        "psr-4": {"Acme\\": "src/"} // "autoloader" : {"namespace": "src"}
    }
}

src based on the project root

Reference