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

New Wave PHP

New Wave PHP

Fosdem PHP Track Talk

Lorna Mitchell

February 01, 2015
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. New Wave PHP

    View Slide

  2. 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 September 2015
    5.5 June 2013 June 2016
    5.6 August 2014 August 2017
    7.0 Q4 2015 Release + 3 months

    View Slide

  3. New Wave PHP

    View Slide

  4. New Wave PHP
    Data from http://w3techs.com

    View Slide

  5. Changes in PHP

    View Slide

  6. Register Globals is Gone (5.4)
    Upgrade PHP and see many undefined
    (simple) variables.
    To fix each undefined variable:
    • replace $username with
    $_GET['username'] or $_POST['username']
    in each case

    View Slide

  7. Echo Shortcut (5.4)
    PHP 5.4 removes short_open_tag config option
    • is never valid - s//• = is always valid

    View Slide

  8. Error Reporting: E_STRICT
    E_ALL includes E_STRICT in PHP 5.4+
    You can either:
    1. Fix your errors
    2. Turn it off again using
    E_ALL & ~E_STRICT

    View Slide

  9. Timezone Settings (5.4)
    PHP refuses to guess the timezone and will
    warn you.
    It is not safe to rely on the system's timezone settings.
    You are *required* to use the date.timezone setting or the
    date_default_timezone_set() function.
    In case you used any of those methods and you are still getting this
    warning, you most likely misspelled the timezone identifier.
    Fix it by setting date.timezone in php.ini:
    date.timezone = "Europe/London"

    View Slide

  10. Call Time Pass By Reference
    PHP Fatal error: Call-time pass-by-reference has been removed
    This error says "don't pass references in
    when the function didn't expect them, you
    fool"

    View Slide

  11. Call Time Pass By Reference
    You can pass by reference. Declare function:
    function inspire(&$person) {
    // be inspiring
    }
    Call with just $ and not &
    inspire($current_user);

    View Slide

  12. Traits (5.5)
    Traits are building blocks for classes
    Traits can:
    • build on one another
    • be used together
    • are inherited by child classes

    View Slide

  13. Traits
    namespace Silex\Application;
    use Monolog\Logger;
    trait MonologTrait {
    public function log($message,
    array $context = array(),
    $level = Logger::INFO) {
    return $this['monolog']->addRecord($level,
    $message, $context);
    }
    }

    View Slide

  14. Traits
    To use a trait:
    use Silex\Application;
    class MyApplication extends Application
    {
    use Application\MonologTrait;
    $app->log("Something went wrong");

    View Slide

  15. DateTimeImmutable (5.5)
    $d1 = new DateTime();
    print_r($d1);
    $d1->add(new DateInterval('P4D'));
    print_r($d1);
    DateTime Object (
    [date] => 2015-01-09 10:43:26
    ...
    )
    DateTime Object (
    [date] => 2015-01-13 10:43:26
    ...
    )

    View Slide

  16. DateTimeImmutable
    $d2 = new DateTimeImmutable();
    print_r($d2);
    $d2->add(new DateInterval('P4D'));
    print_r($d2);
    DateTime Object (
    [date] => 2015-01-09 10:43:26
    ...
    )
    DateTime Object (
    [date] => 2015-01-09 10:43:26
    ...
    )

    View Slide

  17. Easy Password Hashing (5.5)
    $pass = "secretpassword";
    $hashed = password_hash($pass, PASSWORD_DEFAULT);
    echo $hashed;
    Output:
    $2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C63vBSfflQtfjs52GxhAc.s/G
    (you may need to make your password
    columns larger)

    View Slide

  18. 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_compa
    t

    View Slide

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

    View Slide

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

    View Slide

  21. MySQL is Deprecated (5.5)
    The original mysql extension now emits
    E_DEPRECATED warnings.
    Instead, use:
    • PDO
    • The mysqli extension, which is similar
    to the original mysql one

    View Slide

  22. Built-in OpCache (5.5)
    Replaces APC - beware this is disabled by
    default.
    Turn on opcache.enable and
    opcache.enable_cli

    View Slide

  23. Variadic Functions (5.6)
    function concatenate($transform, ...$strings) {
    $string = '';
    foreach($strings as $piece) {
    $string .= $piece;
    }
    return($transform($string));
    }
    echo concatenate("strtoupper", "I'd ", "like ",
    4 + 2, " apples");

    View Slide

  24. Argument Unpacking (5.6)
    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 Slide

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

    View Slide

  26. Namespaced Functions (5.6)
    Have always been able to namespace
    functions
    Can now import them, like classes
    include "Lib/Many/Layers/functions.php";
    use function \Lib\Many\Layers\deep_func;
    deep_func();

    View Slide

  27. Upgrading An Existing System

    View Slide

  28. Performance Comparison

    View Slide

  29. How To Upgrade
    • Turn on E_DEPRECATED and watch the
    logs of your existing platform
    • Use the PHPCompatibility PHPCS
    standard
    • Compile new PHP and run test suite
    • Lint check with new version (php -l)
    • Run application with PHP's webserver
    • Upgrade a test/staging platform
    • Go for it!

    View Slide

  30. PHP's Own Webserver (5.4)
    php -S localhost:8080

    View Slide

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

    View Slide

  32. Finding Platforms
    New projects should be PHP 5.5 or later.
    Questions to ask:
    • What versions of PHP are available?
    • Are backups included?
    • Which extensions are available, and
    can I add others?
    • Can I get support with my PHP setup?

    View Slide

  33. Unofficial List of Providers
    • Servergrove
    http://www.servergrove.com/
    • Linode https://www.linode.com/
    • Digital Ocean
    https://www.digitalocean.com/
    • Siteground
    http://www.siteground.com/
    • Rackspace http://rackspace.com

    View Slide

  34. Questions? (and resources)
    Intermediate PHP http://lrnja.net/php-video
    Contact me: @lornajane or http://lornajane.net

    View Slide