DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Getting Started With Composer In PHP

Getting Started With Composer In PHP

Paul Underwood user avatar by
Paul Underwood
·
Sep. 04, 14 · Interview
Like (0)
Save
Tweet
Share
5.29K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial we are going to learn about composer and how we can use it to develop our PHP applications.

Composer is a tool that allows to manage your third party dependencies in your PHP application, it will allow you to create a list of all third party scripts and libraries and their version number. Composer will then look into the repository of the third party library and the current version number and will automatically download the library into your application.

Composer needs to be told exactly what libraries it requires to install per project, it will then install these libraries into a default vendor folder. An advantage of composer is that it allows you to set dependencies on the libraries.

You can also setup composer to just use defined versions or you can use a wildcard on the versions and composer will get the latest version of the library. Therefore running the update script once a day will ensure you have the latest version of the library code.

Installing Composer

Composer requires that you have PHP 5.3.2 or higher installed, when you run through the installation process there will be other PHP settings that you will need to turn on such as the php_openssl extension.

Depending on the repository type you will need different things install, for example if the repository is in GIT then you will need to have GIT installed.

For information on how to install composer on your local machine you can see this on the composer site.

The easiest way to install composer is by downloading it directly.

curl -sS https://getcomposer.org/installer | php

How To Use Composer

To use composer you need to first create the composer.json file with all the dependencies you need in your application. This will be populated with a JSON object of the dependencies that need to be included in the application.

The first thing you need to know about is using the require keyword this tells composer that your application requires this package to run.

{
    "require": {
        "php": ">=5.3.33",
        "zendframework/zendframework": "2.3.*",
    }
}

This tells composer that the application needs to have PHP of 5.3.33 or higher installed and that it requires Zend Framework 2.3.*, using the wildcard will automatically update up until version 2.4.

Once the composer.json file is in the directory you can install the libraries by running the commands.

php composer.phar install

Default Composer Packages

There is a massive list of third party packages that you can use by default with composer, to see if the package that you need is available to use with composer then you can browser for it on Packagist.org.

Autoloading Your Dependencies

One of my favourite favourites that you get with composer is the autoload file. Inside the vendor folder there is a created file called autoload.php, if this is included in application then you simply need to instantiate the class of the library and this file will autoload it for you.

require_once 'vendor/autoload.php';


PHP Composer (software)

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fargate vs. Lambda: The Battle of the Future
  • Introduction To OpenSSH
  • 10 Things to Know When Using SHACL With GraphDB
  • The 5 Books You Absolutely Must Read as an Engineering Manager

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: