$30 off During Our Annual Pro Sale. View Details »

Upgrading PHP

Upgrading PHP

Adoption of PHP does lag behind the release schedule by more years than it should, but upgrading your platform is wicked hard. This presentation mixes the pleasures of upgrading with the pains of doing so, by showing off new features in the last few versions of PHP and then talking about how to upgrade to that version without any mishaps. Presented at PHP tek 2013.

Lorna Mitchell

May 17, 2013
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. Pains and Gains of PHP
    Upgrades
    Lorna Mitchell, php|tek 2013

    View Slide

  2. About Recent Versions of PHP
    Version Released End of Life
    5.2 November 2006 January 2011
    5.3 June 2009 5.5 release + 1 year
    5.4 March 2012
    5.5 ? ?

    View Slide

  3. Current PHP Installations

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  7. to PHP 5.3

    View Slide

  8. PHP 5.3 is Full of Goodness
    • namespaces

    View Slide

  9. 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 Slide

  10. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  15. 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 Slide

  16. 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 Slide

  17. to PHP 5.4

    View Slide

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

    View Slide

  19. PHP Performance

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  23. 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 Slide

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

    View Slide

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

    View Slide

  26. 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 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 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 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 Slide

  30. to PHP 5.5

    View Slide

  31. PHP 5.5: Better Again
    • password hashing features

    View Slide

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

    View Slide

  33. 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 Slide

  34. PHP 5.5: Better Again
    • password hashing features
    • generators

    View Slide

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

    View Slide

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

    View Slide

  37. PHP 5.5: Better Again
    • password hashing features
    • generators
    • try/catch/finally

    View Slide

  38. 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 Slide

  39. View Slide

  40. PHP 5.5: Better Again
    • 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 Slide

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

    View Slide

  42. In Summary

    View Slide

  43. Upgrade, FFS

    View Slide

  44. 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 Slide

  45. Any Questions?
    Feedback: https://joind.in/8182
    Contact me
    • @lornajane
    • http://lornajane.net

    View Slide

  46. Appendices

    View Slide

  47. PHP Version Adoption
    • Version 5.3 49.2%
    • Version 5.2 44.0%
    • Version 5.4 3.9%
    • Version 5.1 2.8%
    • Version 5.0 0.1%
    • Version 5.5 < 0.1%
    From http://w3techs.com, 5th May 2013

    View Slide

  48. PHP Performance Comparisons
    Average time to run bench.php on my laptop
    • 5.2.17: 3.77 seconds
    • 5.3.23: 2.63 seconds
    • 5.4.15: 1.98 seconds
    • 5.5RC1: 2.11 seconds

    View Slide