InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Installing Composer Packages in PHP
PHP

Installing Composer Packages in PHP

InfinityCoder December 27, 2016

You want to install packages using Composer.

Use Composer’s require command:

1
% php composer.phar require vendor/package:version

For example, to install the latest 2.x version of the PHP_CodeCoverage library from PHPUnit:

1
% php composer.phar require phpunit/php-code-coverage:2.*

Or use a composer.json file:

1
2
3
4
5
{
  "require" : {
    "phpunit/php-code-coverage": "2.*"
  }
}

with Composer’s install command:

1
% php composer.phar install

Composer reads instructions from a file, composer.json, to calculate a set of packages you want it to install.
This file is a simple JSON document. The most important element is the require key.

 

This tells Composer which packages you need to have. For example:

1
2
3
4
5
{
   "require": {
      "phpunit/php-code-coverage": "2.1.*"
   }
}

This says you require the package php-code-coverage published by phpunit, and you’re willing to take any version, as long as it’s somewhere in the 2.1s.
Create this file using any text editor, or use Composer itself with the require command:

1
% php composer.phar require vendor/package:version

This creates (or edits) the file and adds the necessary JSON.
Composer uses a combination of vendor and package as a simple way to namespace packages. Many people have created packages with the same basic names, such as log or json or db.

This lets you specify exactly which one.
In some cases, the vendor and the package have the same name. For example, guzzle/ guzzle. That’s okay.
The composer.json file can contain multiple packages with more sophisticated instructions:

1
2
3
4
5
6
7
{
    "require": {
        "phpunit/php-code-coverage": "2.1.*",
        "guzzle/guzzle": ">=3.7.0",
        "monolog/monolog": "1.7.0"
    }
}

This asks for any 2.1.x version of phpunit/php-code-coverage (but less than 2.2.0), any version of guzzle/guzzle 3.7.0 or higher (including 3.8 and 4.0), and only version 1.7.0 of monolog/monolog (and nothing else).
To trigger the install, use the install command:

1
% php composer.phar install

These packages may have their own dependencies, which the vendor specifies in its own composer.json file.

This includes a version of PHP, specific PHP extensions (such as cURL), and other packages (such as a basic logging class).
During installation, Composer automatically checks your system for these requirements.
If you don’t meet them, it will attempt to fix this (by downloading packages) or complain (if you need to upgrade PHP).
By convention, Composer places all installed packages inside a vendor folder in the current working directory. This keeps everything in one place and allows you to easily add this folder to your .gitignore file.
After installation, Composer writes out a file named composer.lock with the exact set of packages and version it installed.

This allows you to “lock in” the particular set of packages that work for your application.

That way, in case you encounter some unexpected change in one of the packages when you upgrade, you can always recover to a “known good” set.
To use a package, require Composer’s standard autoloader code at the top, then declare the namespace of your package, and instantiate the object.

This is the beginning of a script that uses the Guzzle HTTP Client:

1
2
3
4
5
6
7
8
require 'vendor/autoload.php';
 
use Guzzle\Http\Client;
 
// Create a client to work with the LinkedIn API
$client = new Client('https://api.linkedin.com/{version}', array(
   'version' => 'v1'
));

The mystery of how Composer can have a short autoloader class, yet still manage to find all the packages, is solved through standards.
Two separate packages found on Packagist don’t promise any form of similar design or architecture. However, many packages do commit to various levels of interoperability.

They do so by implementing a set of standards defined by a working group of developers of PHP Frameworks.
PSRs, for PHP Standards Recommendation, lets you know how two packages will behave with each other. The most critical one is PSR-0, the autoloading standard.

When you have many packages, each with its own file naming and directory syntax, it’s not easy to know how to properly require them into your code.
Packages that implement PSR-0 agree to a common set of conventions for namespaces and how their PHP files, and the directories they’re located in, are named and organized.
This allows any PSR-0–compatible package to safely live alongside every other PSR-0 package and to be loaded using one common autoload function.
All packages managed by Composer follow this standard. This allows Composer to make it easy for you to use them.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Checking If a Host Is Alive in PHP
You want to ping a host to see if it …

Checking If a Host Is Alive in PHP

Preventing Global Variable Injection in PHP
You are using an old version of PHP and want …

Preventing Global Variable Injection in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap