Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Composing a Better Phuture

Composing a Better Phuture

Composer is both a dependency management and autoloader generation tool for PHP. This presentation highlights how and why we use Composer.

Jeremy Lindblom

October 02, 2015
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. Dependency Management • Declare, resolve, and install project-specific dependencies •

    Use others’ code ◦ Use open source packages ◦ Focus on your biz features ◦ Avoid “NIH syndrome” • Create reusable packages in your own org
  2. Before Composer • Compile PHP with extensions • System-level package

    managers • PEAR packages (system-system) • Include 3rd-party code in source • Git submodules Composer inspired by bundler and npm
  3. Specifying Versions MAJOR.MINOR.PATCH (SemVer.org) “{vendor}/{package}”: “{version constraints}” • “>=3.1.4” –

    Use any version >= 3.1.4 • “3.1.*” – Use any patch version of 3.1 • “~3.2” – Use any 3.x version >= 3.2
  4. Installing Your Dependencies For development: composer install and composer update

    For production: composer install --no-dev --optimize-autoloader
  5. How do you use your installed code? <?php require ‘vendor/autoload.php’;

    $client = new GuzzleHttp\Client(); $response = $client->get(‘http://example.com’);
  6. a.php CODE CODE include ‘b.php’; CODE Includes = Why I

    Started Using PHP b.php CODE CODE CODE CODE Executed code CODE CODE CODE CODE CODE CODE CODE
  7. Including Classes - Not Fun <?php // NOTE: Assuming one

    class per file require_once(‘classes/user.class.php’); require_once(‘classes/page.class.php’); require_once(‘classes/role.class.php’); require_once(‘classes/db.class.php’); // ...
  8. The Bottom Line Explicit includes/requires are: 1. Necessary and helpful

    for procedural code. 2. Cumbersome and harmful for object- oriented code.
  9. What is an Autoloader? 1. A callback triggered when a

    non-existent class is referenced.
  10. What is an Autoloader? 1. A callback triggered when a

    non-existent class is referenced. 2. A function that provides a definition for a specified class name.
  11. Autoloader Advantages • Classes are loaded on demand ◦ No

    need to write specific include statements ◦ Class never loaded unless it’s needed • Leads to better code maintainability • Easy to write
  12. Simple Autoloader Implementation class Autoloader { public function __construct($root) {...}

    public function load($class) { $path = $path->root; $path .= str_replace(‘\\’, ‘/’, $class); if (file_exists($path)) require $path; } }
  13. Autoloader Implementation Tips • You can have multiple autoloaders. •

    Don’t throw errors/exceptions if a file doesn’t exist. Let the other autoloaders try. • Don’t use the __autoload() function. • Use Composer’s autoloader. ◦ You don’t have to write your own ◦ It already follows the PSR autoloading standards
  14. • FIG = Framework Interop Group • PSR = PHP

    Standards Recommendation ◦ Autoloading: PSR-0, PSR-4 ◦ Others: ▪ PSR-1, PSR-2 - Coding style ▪ PSR-3 - Logging interface ▪ PSR-7 - HTTP message interface PHP-FIG
  15. Autoloader Implementation Revisited class Autoloader { public function __construct($root) {...}

    public function load($class) { $path = $path->root; $path .= str_replace(‘\\’, ‘/’, $class); if (file_exists($path)) require $path; } }
  16. Autoloader Implementation Revisited class Autoloader { public function __construct($root) {...}

    public function load($class) { $path = $path->root; $path .= str_replace(‘\\’, ‘/’, $class); if (file_exists($path)) require $path; } }
  17. Source root base namespace: Zend\Http Class to load: Zend\Http\Client Include

    file: <source-root>/Client.php Autoloading PSRs (PSR-4)
  18. Classmap Autoloading $paths = [...]; // class => file mappings

    if (isset($paths[$class])) require $paths[$class]; // vs. $path = $root . str_replace(‘\\’, ‘/’, $class); if (file_exists($path)) require $path;