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

What's new in PHP 7?

Mattias Geniar
December 03, 2015

What's new in PHP 7?

PHP 7 has been released! During the PHP 7 Release Party in Antwerp I gave a presentation on "What's new in PHP 7?" to highlight the new features, the removed features and what to expect in terms of performance.

Mattias Geniar

December 03, 2015
Tweet

More Decks by Mattias Geniar

Other Decks in Technology

Transcript

  1. WHAT'S NEW IN PHP 7?
    PHP LOST COUNT - WHAT HAPPENED TO PHP 6?
    WHO CARES? PHP 7! 7!!
    @mattiasgeniar
    PHP 7 Release Party @ Nucleus

    View Slide

  2. WHAT'S THIS TALK ABOUT?
    History: PHP 6
    New features in PHP 7
    Removed features
    Q & A
    Some Nucleus 'announcements'
    Raf e?

    View Slide

  3. WHO AM I?
    Mattias Geniar
    System Engineer / Support Lead @
    Former dev, mostly Ops now
    Strong advocate of #DevOps
    Blogger at and
    Nucleus.be
    ma.ttias.be www.nucleus.be

    View Slide

  4. CRON.WEEKLY
    Weekly newsletter with linux & open source
    news, tools,
    tips & projects
    www.cronweekly.com

    View Slide

  5. SPECIAL SHOUT-OUT
    Big thanks to Tom Schuermans ( )
    for his help in the presentation & organisation!
    @tschuermans

    View Slide

  6. HISTORY: LOSING TRACK, WHAT
    ABOUT PHP 6?
    Development started in 2015
    ... and died in 2010
    "The Great Unicode Release"
    Besides unicode, all features went to 5.x release
    5.3 was pretty much PHP 6 without unicode

    View Slide

  7. PHP 7: WHAT'S NEW?
    Scalar type declarations
    Return type declarations
    null coalesce operator
    Spaceship operator
    Arrays in constant de ne()'s
    Anonymous classes

    View Slide

  8. PHP 7: WHAT'S NEW? (CONT'D)
    Unicode codepoint escape syntax & IntlChar
    Filtered (whitelisted) unserialize()
    Grouped namespacing in use
    Session improvements

    View Slide

  9. SCALAR TYPE DECLARATIONS
    // Coercive mode
    function sumOfInts(int ...$ints)
    {
    return array_sum($ints);
    }
    var_dump(sumOfInts(2, '3', 4.1));
    // Outputs
    int(9);
    New in PHP 7: string, integer, oat, boolean

    View Slide

  10. strict mode
    // Strict mode
    declare(strict_types=1);
    function sumOfInts(int ...$ints) { return array_sum($ints); }
    var_dump(sumOfInts(2, '3', 4.1));
    // Outputs
    PHP Fatal error: Uncaught TypeError: Argument 2 passed to sumOf
    must be of the type integer, string given, called in test2.php o
    line 10 and defined in test2.php:5
    Stack trace:
    #0 test2.php(10): sumOfInts(2, '3', 4.1)
    #1 {main}

    View Slide

  11. RETURN TYPE DECLARATIONS
    function sum($a, $b): float {
    return $a + $b;
    }
    // Result is a float, even with integer input
    var_dump(sum(1, 2));
    float(3)

    View Slide

  12. strict mode
    declare(strict_types=1);
    function sum($a, $b): int {
    return $a + $b;
    }
    // Result is a fatal error, float vs integer mismatch
    var_dump(sum(1, 2.5));
    PHP Fatal error: Uncaught TypeError: Return value of sum()
    must be of the type integer, float returned in test3.php:
    Stack trace:
    #0 test3.php(11): sum(1, 2.5)
    #1 {main}

    View Slide

  13. 'STRICT MODE' GOTCHAS
    Enabled/disabled per le, not a php.ini setting
    applies to function calls, not to the functions
    declarations
    (so: if de ned in test.php, all function calls made from test.php have strict typing)
    Integers are allowed for functions requiring oat

    View Slide

  14. NULL COALESCE OPERATOR
    // PHP < 7
    $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'
    // PHP >= 7
    $username = $_GET['user'] ?? 'nobody';
    // Chained: $_GET, then $_POST, then 'nobody'
    $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'

    View Slide

  15. SPACESHIP OPERATOR

    View Slide

  16. SPACESHIP OPERATOR
    It returns -1, 0 or 1 when $a is respectively less than,
    equal to, or greater than $b
    echo 1 <=> 1; // 0
    echo 1 <=> 2; // -1
    echo 2 <=> 1; // 1
    // Example: usort callbacks
    // Note: also works on characters
    usort($data, function ($left, $right) {
    return $left[1] <=> $right[1];
    });

    View Slide

  17. ARRAYS IN CONSTANTS
    define('ANIMALS', ['dog', 'cat', 'bird']);
    echo ANIMALS[1]; // outputs "cat"
    ?>

    View Slide

  18. ANONYMOUS CLASSES
    $app = new Application;
    // 'new class' creates anonymous class
    $app->setLogger(new class implements Logger {
    public function log(string $msg) {
    echo $msg;
    }
    });

    View Slide

  19. ANONYMOUS FUNCTIONS (PHP >=
    5.3)
    $example = function ($text) {
    echo $text;
    };
    $example('I want that PHP 7 elephpant!');

    View Slide

  20. UNICODE
    echo "\u{aa}";
    // Output in PHP < 7
    \u{aa}
    // Output in PHP >= 7
    ª

    View Slide

  21. YOU KNOW WHAT THAT MEANS ...
    echo "\u{1F61D}";
    Emoji!

    View Slide

  22. FILTERED / WHITELISTED
    UNSERIALIZE()
    // converts all objects into __PHP_Incomplete_Class object
    $data = unserialize($foo, [
    "allowed_classes" => false
    ]);
    // converts all objects into __PHP_Incomplete_Class object
    // except those of MyClass and MyClass2
    $data = unserialize($foo, [
    "allowed_classes" => ["MyClass", "MyClass2"]
    ]);

    View Slide

  23. GROUPED USE DECLARATIONS
    use const some\namespace\ConstA;
    use const some\namespace\ConstB;
    use const some\namespace\ConstC;
    // PHP 7+ code
    use const some\namespace\{ConstA, ConstB, ConstC};

    View Slide

  24. SESSION IMPROVEMENTS
    session_start() accepts array of options
    (undocumented) option "read_and_close": stops
    session blocking!
    // PHP 7:
    session_start([
    'read_and_close' => true,
    ]);
    // PHP <= 7
    session_write_close();

    View Slide

  25. REMOVED FEATURES & EXTENSIONS
    IN PHP 7
    plain 'mysql' extension removed (mysqli / pdo only
    from now on)
    php.ini: removed "#" comments, only ";" from now
    on
    Removed asp_tags:
    Finally removed ereg() (deprecated since 5.3)
    preg_replace() no longer supports "\e"

    View Slide

  26. REMOVED FEATURES & EXTENSIONS
    IN PHP 7 (PART DEUX)
    Finally removed
    set_magic_quotes_runtime() (deprecated
    since 5.3)
    Removed
    always_populate_raw_post_data in php.ini

    View Slide

  27. UNIFORM VARIABLE SYNTAX
    // PHP < 7
    $object->$array['item'];
    --> $object->{$array['item']};
    // PHP >= 7
    $object->$array['item'];
    --> ${object->$array}['item'];

    View Slide

  28. DATE.TIMEZONE WARNINGS
    REMOVED
    default still UTC
    // PHP < 7
    Strict Standards: date(): It is not safe to rely on the
    system timezone settings. ....
    // PHP >= 7
    (nothing)

    View Slide

  29. SAPI TRIVIA
    How many ways are there to run PHP?
    PHP 5.6: 20+
    PHP 7.0: 6
    PHP 7.0 removed:
    aolserver, apache, apache_hooks, apache2 lter, caudium, continuity isapi, milter, nsapi,
    phttpd, pi3web, roxen, thttpd, tux, webjames
    PHP 7.0 supports: embedded, apache2 module,
    fpm, cgi, cli, litespeed

    View Slide

  30. PERFORMANCE 7 VS 5.6
    Test sites
    http://phpseven.lin5.nucleus.be/wordpress/
    http://php ve.lin5.nucleus.be/wordpress/
    Basic Wordpress, no tweaks or con g changes
    $ ab -c 1 -n 100
    http://site/wordpress
    PHP 7.0: 12.9req/s (75ms per request)
    PHP 5.6: 8.9 req/s (120ms per request)
    PHP 5.5: 2.5 req/s (450ms per request)

    View Slide

  31. PERFORMANCE OF PHP 7 IN ONE
    SLIDE

    View Slide

  32. PERFORMANCE OF PHP 7 IN ONE
    SLIDE
    6x faster than PHP 5.5
    1.6 faster than PHP 5.6
    Your mileage may vary

    View Slide

  33. Q & A
    (hold on, we've got more coming)

    View Slide

  34. SOME NUCLEUS ANNOUNCEMENTS
    100gr of PHP 7 for everyone!
    (conveniently packed in a 16GB USB disk with the PHP 7 source code on it)

    View Slide

  35. SOME NUCLEUS ANNOUNCEMENTS
    As of today, PHP 7 support on our shared hosting!
    Let's merge this feature live!

    View Slide

  36. SOME NUCLEUS ANNOUNCEMENTS
    Free PHP 7 shared hosting for everyone here!
    (give me your e-mail address, I'll hook you up)

    View Slide

  37. PHP 7 ELEPHPANT RAFFLE
    Time to bribe Mike!

    View Slide

  38. THANKS!
    Contact via @mattiasgeniar or via [email protected]

    View Slide