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

Upgrading PHP

Upgrading PHP

Slide deck for DrupalCampScot and phpDay, although the talks themselves were quite different!

Lorna Mitchell

May 10, 2014
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. Pains and Gains of PHP
    Upgrades
    Lorna Mitchell, May 2014

    View full-size slide

  2. About Recent Versions of PHP
    Version Released End of Life
    5.2 November 2006 January 2011
    5.3 June 2009 July 2014
    5.4 March 2012 Release + 3 years
    5.5 June 2013 Release + 3 years
    5.6 Soon Release + 3 years

    View full-size slide

  3. Current PHP Installations

    View full-size slide

  4. Pains of Upgrades
    • cost (time and money)
    • risk

    View full-size slide

  5. Gains of Upgrades
    • shiny new features
    • better security
    • better performance

    View full-size slide

  6. Not All Upgrades Are Equal
    • to 5.3: climbing a mountain
    • to 5.4: flying leap
    • to 5.5: nobody will notice
    • to 5.6: another anticlimax

    View full-size slide

  7. PHP 5.3 is Full of Goodness
    • namespaces

    View full-size slide

  8. PHP Namespaces
    Namespaces allow modularity and separation
    • classes, constants and functions belong in
    namespaces
    • namespaces can be nested
    • much easier pick-and-mix of
    frameworks/libraries
    • avoids naming collision
    • key ingredient for PSR-0

    View full-size slide

  9. Namespaced Library File
    namespace Lorna;
    class Nonsense {
    protected $words =
    array("wibble", "squeak", "howl", "pop");
    public function speak() {
    return $this->words[
    array_rand($this->words)];
    }
    }

    View full-size slide

  10. Namespaced Calling Code
    include 'lorna/nonsense.php';
    use Lorna\Nonsense;
    $nsense = new Nonsense();
    echo $nsense->speak();
    $other = new \StdClass();

    View full-size slide

  11. PHP 5.3 is Full of Goodness
    • namespaces
    • anonymous functions

    View full-size slide

  12. Anonymous Functions
    $things = array("name" => "Alice",
    "colour" => "Blue",
    "origin" => "Dreams");
    function formatit($value, $key) {
    echo $key . ": " . $value . "\n";
    }
    array_walk($things, "formatit");

    View full-size slide

  13. Anonymous Functions
    $things = array("name" => "Alice",
    "colour" => "Blue",
    "origin" => "Dreams");
    array_walk($things, function ($value, $key) {
    echo $key . ": " . $value . "\n";
    });

    View full-size slide

  14. PHP 5.3 is Full of Goodness
    • namespaces
    • anonymous functions
    • better DateTime
    • SPL data structures
    • works on Windows
    • E_DEPRECATED error reporting level
    • late static binding
    • __DIR__

    View full-size slide

  15. Upgrading to 5.3: Gotchas
    new reserved words: namespace, goto
    log message changes
    requires MySQL > 4.1
    full info: http://lrnja.net/ZpvknE (php.net)

    View full-size slide

  16. PHP 5.4 is a Leap to the Future
    • improved performance, reduced memory
    footprint

    View full-size slide

  17. PHP Performance

    View full-size slide

  18. PHP 5.4 is a Leap to the Future
    • improved performance, reduced memory
    footprint
    • built-in webserver

    View full-size slide

  19. PHP's Own Webserver
    php -S dev.project.local:8080
    -t /var/www/cool-project/web
    -c php-ini.development
    routing.php

    View full-size slide

  20. PHP 5.4 is a Leap to the Future
    • improved performance, reduced memory
    footprint
    • built-in webserver
    • traits

    View full-size slide

  21. Simple Traits Example
    trait Audit {
    public function getAuditTrail() {
    return "nothing changed";
    }
    }
    class Princess {
    use Audit;
    // General princess class description:
    // soldering, tree climbing, the usual
    }

    View full-size slide

  22. Simple Traits Example
    $daisy = new Princess();
    echo $daisy->getAuditTrail();

    View full-size slide

  23. PHP 5.4 is a Leap to the Future
    • improved performance, reduced memory
    footprint
    • built-in webserver
    • short array notation, array dereferencing

    View full-size slide

  24. Short Array Notation
    $game[0] = 'paper';
    $game[1] = 'scissors';
    $game[2] = 'stone';
    $game = array(0 => 'stone',
    1 => 'paper',
    2 => 'scissors');
    $game = [0 => 'scissors', 1 => 'stone', 2 => 'paper'];

    View full-size slide

  25. PHP 5.4 is a Leap to the Future
    • improved performance, reduced memory
    footprint
    • built-in webserver
    • short array notation, array dereferencing
    • echo shortcut always available

    View full-size slide

  26. PHP 5.4 Echo Shortcut
    PHP 5.4 removes short_open_tag config option
    • is never valid
    • = is always valid
    A template saying "hi, "
    is the same as:
    A template saying "hi, =$name ?>"

    View full-size slide

  27. PHP 5.4 is a Leap to the Future
    • improved performance, reduced memory
    footprint
    • built-in webserver
    • traits
    • short array notation, array dereferencing
    • less nonsense

    View full-size slide

  28. Upgrading to PHP 5.4: Gotchas
    new reserved words: trait callable insteadof
    no register_globals, short open tag etc
    turn on E_DEPRECATED on your 5.3 platform
    compile PHP 5.4 and use the webserver

    View full-size slide

  29. Upgrading to PHP 5.4: Gotchas
    new reserved words: trait callable insteadof
    no register_globals, short open tag etc
    turn on E_DEPRECATED on your 5.3 platform
    compile PHP 5.4 and use the webserver
    beware APC issues

    View full-size slide

  30. PHP 5.5: New Features
    • password hashing features

    View full-size slide

  31. Easy Password Hashing
    $pass = "secretpassword";
    $hashed = password_hash($pass, PASSWORD_DEFAULT);
    echo $hashed;
    $2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C63vBSffl

    View full-size slide

  32. Easy Password Hashing
    $existing_hash = '$2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C
    $pass = "secretpassword";
    if(password_verify($pass, $existing_hash))
    echo "All good";
    else
    echo "Go away";
    For PHP < 5.5:
    http://github.com/ircmaxell/password_compat

    View full-size slide

  33. PHP 5.5: New Features
    • password hashing features
    • generators

    View full-size slide

  34. PHP Generators
    Simpler than iterators - a function returning a
    sequence of values.
    function getValues() {
    // totally trivial example
    yield "Apple";
    yield "Ball";
    yield "Cat";
    }

    View full-size slide

  35. PHP Generators
    Simpler than iterators - a function returning a
    sequence of values.
    $stuff = getValues();
    foreach($stuff as $thing) {
    echo $thing . "\n";
    }

    View full-size slide

  36. PHP 5.5: New Features
    • password hashing features
    • generators
    • try/catch/finally

    View full-size slide

  37. Try/Catch/Finally
    Finally is a clause that always gets run
    try{
    $fp = fopen("textfile.txt", "w+");
    // do stuff
    } catch (Exception $e) {
    echo "Stuff went wrong";
    } finally {
    // either way, close the file
    fclose($fp);
    }

    View full-size slide

  38. PHP 5.5: New Features
    • password hashing features
    • generators
    • try/catch/finally
    • built-in opcode cache
    • Turn on opcache.enable and
    opcache.enable_cli
    • use APCu
    https://github.com/krakjoe/apcu

    View full-size slide

  39. Upgrading to PHP 5.5
    • turn on E_DEPRECATED on your 5.4 platform
    • compile PHP 5.5 and use the webserver

    View full-size slide

  40. PHP 5.6: Just Better
    • variadic functions

    View full-size slide

  41. Variadic Functions
    Variable parameters with func_get_args()
    function concatenate($transform) {
    $string = '';
    $pieces = func_get_args();
    $transform = array_shift($pieces);
    foreach($pieces as $piece) {
    $string .= $piece;
    }
    return($transform($string));
    }
    echo concatenate("strtoupper", "I'd ", "like ",
    4 + 2, " apples");

    View full-size slide

  42. Variadic Functions
    Much cleaner with new syntax:
    function concatenate($transform, ...$strings) {
    $string = '';
    foreach($strings as $piece) {
    $string .= $piece;
    }
    return($transform($string));
    }
    echo concatenate("strtoupper", "I'd ", "like ",
    4 + 2, " apples");

    View full-size slide

  43. PHP 5.6: Just Better
    • variadic functions
    • argument unpacking

    View full-size slide

  44. Argument Unpacking
    Let PHP unpack your args
    $email[] = "[email protected]";
    $email[] = "Hi there";
    $email[] = "Thanks for registering, hope you like it";
    // old syntax
    mail($email[0], $email[1], $email[2]);
    //new syntax
    mail(...$email);

    View full-size slide

  45. Argument Unpacking
    Let PHP unpack your args
    $email[] = "Hi there";
    $email[] = "Thanks for registering, hope you like it";
    mail("[email protected]", ...$email);

    View full-size slide

  46. PHP 5.6: Just Better
    • variadic functions
    • argument unpacking
    • import namespaced functions/constants

    View full-size slide

  47. Import Namespaced Functions
    For classes:
    use \Lib\Many\Layers\Thing;
    $a = new Thing();
    Now also for functions:
    include "Lib/Many/Layers/functions.php";
    use function \Lib\Many\Layers\deep_func;
    deep_func();

    View full-size slide

  48. PHP 5.6: Just Better
    • variadic functions
    • argument unpacking
    • import namespaced functions/constants
    • exponentiation operator

    View full-size slide

  49. Exponentiation Operator
    Like pow() but as an operator
    echo pow(4,3); // 64
    echo 4 ** 3; //64

    View full-size slide

  50. PHP 5.6: Just Better
    • variadic functions
    • argument unpacking
    • import namespaced functions/constants
    • exponentiation operator
    • phpdbg included

    View full-size slide

  51. Upgrading to PHP 5.6
    • turn on E_DEPRECATED on your 5.5 platform
    • compile PHP 5.6 and use the webserver

    View full-size slide

  52. Upgrade, FFS

    View full-size slide

  53. General Upgrade Plan
    • check the changelog:
    http://php.net/ChangeLog-5.php
    • turn on E_DEPRECATED
    • test with the webserver, run unit tests
    with new binary
    • upgrade test platform
    • pecl extensions will need a reinstall
    • go for it :)

    View full-size slide

  54. Any Questions?
    Contact me
    • @lornajane
    • http://lornajane.net

    View full-size slide

  55. PHP Version Adoption
    • Version 5.3 52.1%
    • Version 5.2 28.1%
    • Version 5.4 16.3%
    • Version 5.5 1.9%
    • Version 5.1 1.5%
    (from W3Techs.com, 5 May 2014)

    View full-size slide

  56. PHP Performance Comparisons
    Average time to run bench.php on my laptop
    • 5.3.28: 3.92 seconds
    • 5.4.27: 2.72 seconds
    • 5.5.11: 2.69 seconds
    • 5.6.0beta2: 2.36 seconds

    View full-size slide