SlideShare a Scribd company logo
The wonderful world of
Composer
Transforming from Older PHP to Modern PHP
 PHP community: Huge but extremely isolated libraries, we do not reuse code ,Do we
?.
 Issues faced while using shared code:
 How do I autoload the PHP classes in the code , without using require or include
statements? Which version of file to include according to my installed PHP version ?
 We need to know if this library depends on any other libraries, if YES then that’s yet another library
I need to download & configure
 How should I store the library in my project? Should I use Git submodules? Just
commit the entire lib code into my project?
Composer sets out to solve this situation by positioning itself as "the glue between all projects" -
meaning that packages can be written, developed and shared in a format that other developers
can plug into other applications with ease.
Contents
Introduction & Basic Usage
1. Composer Introduction
2. Installing
3. Basic Usage & commands
4. Packagist Repository
5. Specifying package versions
Namespacing
1. Namespacing Introduction
2. Using the namespaced class
Autoloading you own code
1. Using classmap
2. Using PSR-0 standard
3. Using PSR-4 standard
4. PSR-0 vs PSR-4
Final Summary : Basic Commands & composer.json schema
Introduction
Composer is a dependency manager tool for PHP.
Like npm in Node
Or bundler in Ruby.
Suppose:
• You have a project that depends on a number of libraries.
• Some of those libraries depend on other libraries.
Composer:
• Enables you to declare the libraries you depend on (inside config file).
• Finds out which versions of which packages can and need to be installed, and
installs them (meaning it downloads them into your project).
A Dependency Manager lets you:
1. Define dependencies in a version controlled config file.
2. Download & Install them all in one command
3. Ensures that identical versions are installed in all project environments .
4. Automate this part of your build process.
Action time
Installation
Basic Usage: Require dependencies
Action time
Example1: require command
Generated files
1.
2.
$ composer require monolog/monolog
Basic Usage: Install dependencies from composer.json
a composer.json
$ composer install
Installing
Generated files
1.
2.
3.
Documentation time
Packagist
Two important files
Demo time
Example2: composer install
Warning: lock & json file not synced
This file describes the dependencies of your project and may contain other
metadata as well.
◎ composer.json – (config file)
After installing the dependencies, Composer writes the list of the exact
versions it installed into a composer.lock file. This locks the project to those
specific versions.
This is important because the install command checks if a lock file is
present, and if it is, it downloads the versions specified there (regardless of
what composer.json says).
◎ composer.lock – (lock file)
Specifying versions
{
"require": {
"monolog/monolog": "1.0.2",
"symfony/http-foundation": “1.0.*",
"vendor/package": ">=1.0 <2.0",
"vendor/package": "~1.2"
}
}
Wildcard (*): 1.0.* is the equivalent of >=1.0 <2.0
Range: is the equivalent of >=1.0 <2.0
Next significant release (tilde):
~1.2 is equivalent to >=1.2 < 2.0.0
~1.2.3 is equivalent to >=1.2.3 < 1.3.0
Demo time
semver.mwl.be
Namespacing
Technique for
organizing your code &
prevent naming
conflicts between
classes.
Namespace example
Demo time
Example3: Namespacing
// PSI/Libraries/Calculator.php
namespace PSILibraries;
class Calculator {
public function add($a, $b){
if ( !is_numeric($a) || !is_numeric($b) ) {
throw new InvalidArgumentException;
}
return $a + $b;
}
}
use PSILibrariesCalculator;
$cal = new Calculator();
echo $cal->add(4 ,5);
to instantiate an object of this class , we need to use backslash notation
Autoloading
Autoloading allows us to use PHP classes
without the need to require() or
include() them and is considered a hallmark
of modern-day programming.
Different ways to autoload classes
◎ Classmap
◎ Files
◎ PSR-0
◎ PSR-4
deprecated: recommended to use PSR-4 instead
Questions
Q: PSR ?
Q: PHP-FIG
Autoloading using classmap
a composer.json
$ composer dump-autoload
Generate autoload files
Generated files
1.
2.
3.
Action time
example4: autoload classmap
{
"autoload": {
"classmap": ["src/", "lib/", "Something.php"]
}
}
Autoloading using PSR-0
a composer.json
$ composer dump-autoload
Generate autoload files
Generated files
1.
2.
3.
Action time
example5: autoload via PSR-0
{
"autoload": {
"psr-0": {
"PSI": "src/",
}
}
}
Autoloading using PSR-4
a composer.json
$ composer dump-autoload
Generate autoload files
Generated files
1.
2.
3.
Action time
example6: autoload via PSR-4
{
"autoload": {
"psr-4": {
"PSI": "src/PSI",
}
}
}
Optimize performance using classmap
$ composer dump-autoload --optimize
In production, you can generate a class map for all the
classes, get a faster autoloader and optimize
performance, you can get 20% boost.
Basic Commands & composer.json schema
Name Usage Description
Require composer require vendor-name/package-name
Adds required packages to your
composer.json and installs them.
Install
composer install
Parses the composer.json file and
downloads the needed dependencies.
Update composer update
Updates your dependencies to the
latest version, and updates the
composer.lock file.
Dump-autoload composer dump-autoload --optimize
If you need to update the autoloader
because of new classes in a classmap
package for example, you can use
"dump-autoload" to do that without
having to go through an install or
update.
Use --optimize to convert PSR-0
autoloading to classmap to get a
faster autoloader. This is strongly
recommended for production (you
can get a 20% boost), but can take a
bit of time to run so it is currently not
done by default.
Documentation time
Developer Cheat Sheet
References
Special thanks to all the people who made and released
these awesome resources for free:
◎ https://getcomposer.org/
◎ http://www.php-fig.org/
◎ Optimizing composer autoloader performance
Thanks!
Any questions?
You can find me at:
deepak.chandani@thepsi.com

More Related Content

PPTX
TYPO3 & Composer
PDF
Create a PHP Library the right way
PPTX
Essential Tools for Modern PHP
PDF
Flask for cs students
PDF
How to write a Dockerfile
ODP
Managing Plone Projects with Perl and Subversion
PPTX
C compilation process
PDF
Thesis - LLVM toolchain support as a plug-in for Eclipse CDT
TYPO3 & Composer
Create a PHP Library the right way
Essential Tools for Modern PHP
Flask for cs students
How to write a Dockerfile
Managing Plone Projects with Perl and Subversion
C compilation process
Thesis - LLVM toolchain support as a plug-in for Eclipse CDT

What's hot (20)

PDF
Percona XtraDB Cluster before every release: Glimpse into CI testing
PDF
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
ODP
Lazarus
PPTX
Containers: Anti Pattern
PDF
Sep Nasiri "Upwork PHP Architecture"
PPTX
.NET Core: a new .NET Platform
PDF
Trying to Sell PVS-Studio to Google, or New Bugs in Chromium
ODP
Pipeline based deployments on Jenkins
PDF
Build Great Networked APIs with Swift, OpenAPI, and gRPC
PPTX
Symfony Under Control by Maxim Romanovsky
PDF
Beginning with Composer - Dependency manager in php
PPTX
Report portal
PPTX
Pipeline as code - new feature in Jenkins 2
PDF
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
PDF
走向开源:向CPAN提交模块Step By Step
PDF
LabDocumentation
PDF
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
PDF
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
PPT
Demystifying Maven
Percona XtraDB Cluster before every release: Glimpse into CI testing
차세대컴파일러, VM의미래: 애플 오픈소스 LLVM
Lazarus
Containers: Anti Pattern
Sep Nasiri "Upwork PHP Architecture"
.NET Core: a new .NET Platform
Trying to Sell PVS-Studio to Google, or New Bugs in Chromium
Pipeline based deployments on Jenkins
Build Great Networked APIs with Swift, OpenAPI, and gRPC
Symfony Under Control by Maxim Romanovsky
Beginning with Composer - Dependency manager in php
Report portal
Pipeline as code - new feature in Jenkins 2
IBM ConnectED 2015 - BP106 From XPages Hero To OSGi Guru: Taking The Scary Ou...
走向开源:向CPAN提交模块Step By Step
LabDocumentation
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Knowledge Sharing Session on JavaScript Source Maps & Angular Compilation
Demystifying Maven
Ad

Viewers also liked (20)

PPT
Gifts infinity beer mugs
PPTX
Factors affecting lls
PPTX
Sistem reproduksi pada manusia
PPTX
hukum mendel
PDF
Index IEEE Photonics Journal volume 7 2015
PPSX
Sharaninfotech
PDF
Blockchain summit deck brief v04 (1)
PPTX
バネのモデル コピー
PPTX
Websites
DOC
CV. P.S.MANJUNATHAN
PPTX
Present simple and present continuous
DOC
CV. P.S.MANJUNATHAN
PPTX
Top Disney's Frozen cakes
PPTX
How to Make a Cheats Frozen Cake
PPT
Parkinson (1)
PPTX
Past perfect
PPTX
London
PPTX
Novinki
PDF
Cost management haghpanah-2015
Gifts infinity beer mugs
Factors affecting lls
Sistem reproduksi pada manusia
hukum mendel
Index IEEE Photonics Journal volume 7 2015
Sharaninfotech
Blockchain summit deck brief v04 (1)
バネのモデル コピー
Websites
CV. P.S.MANJUNATHAN
Present simple and present continuous
CV. P.S.MANJUNATHAN
Top Disney's Frozen cakes
How to Make a Cheats Frozen Cake
Parkinson (1)
Past perfect
London
Novinki
Cost management haghpanah-2015
Ad

Similar to Composer namespacing (20)

PDF
Dependency management with Composer
PPTX
PHP Dependency Management with Composer
PDF
composer_talk_20160209
PDF
Composer Helpdesk
PPTX
Composer
PDF
12 Composer #burningkeyboards
PDF
Composer & Drupal
PDF
Php Dependency Management with Composer ZendCon 2016
DOCX
"The Power of Composer"
PPTX
Composer Lightning Talk
PPTX
[Patel] SPFx: An ISV Insight into latest Microsoft's customization model
PDF
Node JS - A brief overview on building real-time web applications
PDF
Php Dependency Management with Composer ZendCon 2017
PPTX
Pemrograman mobile menggunakan ionic framework
PPTX
Tribal Nova Docker feedback
PPTX
Building an Ionic hybrid mobile app with TypeScript
PDF
Development and deployment with composer and kite
PPTX
Composer JSON kills make files
DOCX
Prizm Installation Guide
Dependency management with Composer
PHP Dependency Management with Composer
composer_talk_20160209
Composer Helpdesk
Composer
12 Composer #burningkeyboards
Composer & Drupal
Php Dependency Management with Composer ZendCon 2016
"The Power of Composer"
Composer Lightning Talk
[Patel] SPFx: An ISV Insight into latest Microsoft's customization model
Node JS - A brief overview on building real-time web applications
Php Dependency Management with Composer ZendCon 2017
Pemrograman mobile menggunakan ionic framework
Tribal Nova Docker feedback
Building an Ionic hybrid mobile app with TypeScript
Development and deployment with composer and kite
Composer JSON kills make files
Prizm Installation Guide

Recently uploaded (20)

PDF
August Patch Tuesday
PDF
Encapsulation theory and applications.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Machine Learning_overview_presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
August Patch Tuesday
Encapsulation theory and applications.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Tartificialntelligence_presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Programs and apps: productivity, graphics, security and other tools
Machine Learning_overview_presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
A comparative study of natural language inference in Swahili using monolingua...
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
Building Integrated photovoltaic BIPV_UPV.pdf
Getting Started with Data Integration: FME Form 101
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Group 1 Presentation -Planning and Decision Making .pptx
SOPHOS-XG Firewall Administrator PPT.pptx

Composer namespacing

  • 1. The wonderful world of Composer
  • 2. Transforming from Older PHP to Modern PHP  PHP community: Huge but extremely isolated libraries, we do not reuse code ,Do we ?.  Issues faced while using shared code:  How do I autoload the PHP classes in the code , without using require or include statements? Which version of file to include according to my installed PHP version ?  We need to know if this library depends on any other libraries, if YES then that’s yet another library I need to download & configure  How should I store the library in my project? Should I use Git submodules? Just commit the entire lib code into my project? Composer sets out to solve this situation by positioning itself as "the glue between all projects" - meaning that packages can be written, developed and shared in a format that other developers can plug into other applications with ease.
  • 3. Contents Introduction & Basic Usage 1. Composer Introduction 2. Installing 3. Basic Usage & commands 4. Packagist Repository 5. Specifying package versions Namespacing 1. Namespacing Introduction 2. Using the namespaced class Autoloading you own code 1. Using classmap 2. Using PSR-0 standard 3. Using PSR-4 standard 4. PSR-0 vs PSR-4 Final Summary : Basic Commands & composer.json schema
  • 4. Introduction Composer is a dependency manager tool for PHP. Like npm in Node Or bundler in Ruby. Suppose: • You have a project that depends on a number of libraries. • Some of those libraries depend on other libraries. Composer: • Enables you to declare the libraries you depend on (inside config file). • Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).
  • 5. A Dependency Manager lets you: 1. Define dependencies in a version controlled config file. 2. Download & Install them all in one command 3. Ensures that identical versions are installed in all project environments . 4. Automate this part of your build process. Action time Installation
  • 6. Basic Usage: Require dependencies Action time Example1: require command Generated files 1. 2. $ composer require monolog/monolog
  • 7. Basic Usage: Install dependencies from composer.json a composer.json $ composer install Installing Generated files 1. 2. 3. Documentation time Packagist
  • 8. Two important files Demo time Example2: composer install Warning: lock & json file not synced This file describes the dependencies of your project and may contain other metadata as well. ◎ composer.json – (config file) After installing the dependencies, Composer writes the list of the exact versions it installed into a composer.lock file. This locks the project to those specific versions. This is important because the install command checks if a lock file is present, and if it is, it downloads the versions specified there (regardless of what composer.json says). ◎ composer.lock – (lock file)
  • 9. Specifying versions { "require": { "monolog/monolog": "1.0.2", "symfony/http-foundation": “1.0.*", "vendor/package": ">=1.0 <2.0", "vendor/package": "~1.2" } } Wildcard (*): 1.0.* is the equivalent of >=1.0 <2.0 Range: is the equivalent of >=1.0 <2.0 Next significant release (tilde): ~1.2 is equivalent to >=1.2 < 2.0.0 ~1.2.3 is equivalent to >=1.2.3 < 1.3.0 Demo time semver.mwl.be
  • 10. Namespacing Technique for organizing your code & prevent naming conflicts between classes.
  • 11. Namespace example Demo time Example3: Namespacing // PSI/Libraries/Calculator.php namespace PSILibraries; class Calculator { public function add($a, $b){ if ( !is_numeric($a) || !is_numeric($b) ) { throw new InvalidArgumentException; } return $a + $b; } } use PSILibrariesCalculator; $cal = new Calculator(); echo $cal->add(4 ,5); to instantiate an object of this class , we need to use backslash notation
  • 12. Autoloading Autoloading allows us to use PHP classes without the need to require() or include() them and is considered a hallmark of modern-day programming.
  • 13. Different ways to autoload classes ◎ Classmap ◎ Files ◎ PSR-0 ◎ PSR-4 deprecated: recommended to use PSR-4 instead Questions Q: PSR ? Q: PHP-FIG
  • 14. Autoloading using classmap a composer.json $ composer dump-autoload Generate autoload files Generated files 1. 2. 3. Action time example4: autoload classmap { "autoload": { "classmap": ["src/", "lib/", "Something.php"] } }
  • 15. Autoloading using PSR-0 a composer.json $ composer dump-autoload Generate autoload files Generated files 1. 2. 3. Action time example5: autoload via PSR-0 { "autoload": { "psr-0": { "PSI": "src/", } } }
  • 16. Autoloading using PSR-4 a composer.json $ composer dump-autoload Generate autoload files Generated files 1. 2. 3. Action time example6: autoload via PSR-4 { "autoload": { "psr-4": { "PSI": "src/PSI", } } }
  • 17. Optimize performance using classmap $ composer dump-autoload --optimize In production, you can generate a class map for all the classes, get a faster autoloader and optimize performance, you can get 20% boost.
  • 18. Basic Commands & composer.json schema Name Usage Description Require composer require vendor-name/package-name Adds required packages to your composer.json and installs them. Install composer install Parses the composer.json file and downloads the needed dependencies. Update composer update Updates your dependencies to the latest version, and updates the composer.lock file. Dump-autoload composer dump-autoload --optimize If you need to update the autoloader because of new classes in a classmap package for example, you can use "dump-autoload" to do that without having to go through an install or update. Use --optimize to convert PSR-0 autoloading to classmap to get a faster autoloader. This is strongly recommended for production (you can get a 20% boost), but can take a bit of time to run so it is currently not done by default. Documentation time Developer Cheat Sheet
  • 19. References Special thanks to all the people who made and released these awesome resources for free: ◎ https://getcomposer.org/ ◎ http://www.php-fig.org/ ◎ Optimizing composer autoloader performance