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

What's new in PHP 7? - Coolblue Behind The Scenes 2016

What's new in PHP 7? - Coolblue Behind The Scenes 2016

PHP 7 talk I presented at Coolblue Behind The Scenes.

Go to https://talks.feryn.eu/talks/161/what-s-new-in-php-7-behind-the-scenes-rotterdam to see more details.

Thijs Feryn

July 19, 2016
Tweet

More Decks by Thijs Feryn

Other Decks in Technology

Transcript

  1. By Thijs Feryn
    What's new in

    View Slide

  2. Hi, I’m Thijs

    View Slide

  3. I’m
    @ThijsFeryn
    on Twitter

    View Slide

  4. I’m an
    Evangelist
    At

    View Slide

  5. I’m a
    at
    board member

    View Slide

  6. To know where PHP is heading, you have
    to know what happened in the past

    View Slide

  7. PHP 7.0: December 3rd 2015
    PHP 5.6: 2014
    PHP 5.5: 2013
    PHP 5.4: 2012
    PHP 5.3: 2009
    PHP 5.2: 2006
    PHP 5.1: 2005
    PHP 5.0: 2004

    View Slide

  8. We've been
    using PHP 5 for
    11 years

    View Slide

  9. Why is there no
    PHP 6?

    View Slide

  10. Windows 10?

    View Slide

  11. PHP 6 did once exist

    View Slide

  12. But was never
    released

    View Slide

  13. A history
    lesson

    View Slide

  14. First ideas in 2005
    by Andrei Zmievski
    at Yahoo

    View Slide

  15. PHP 6 was all about
    unicode support

    View Slide

  16. And some extra
    language features

    View Slide

  17. ✓Namespaces
    ✓Late static binding
    ✓Goto labels
    ✓Object casting to primitives
    ✓Type hinting
    ✓APC
    ✓Non-fatal errors should become exceptions
    PHP 6 extra language features

    View Slide

  18. Delays, delays, delays …

    View Slide

  19. Most
    non-Unicode features
    were released in PHP 5.3

    View Slide

  20. The rest was eventually
    shipped off to PHP 5.4

    View Slide

  21. View Slide

  22. ✓UTF-16 or UTF-8? Or even UTF-32?
    ✓Few people know intricacies of Unicode & ICU
    ✓Major performance penalty (300%)
    ✓Too much effort to port all code
    PHP 6 unicode difficulties

    View Slide

  23. Controversy

    View Slide

  24. In 2010 the core team
    concluded that
    Unicode support for
    PHP was too hard to
    implement

    View Slide

  25. Ah, Jani went a little crazy today in his typical style to force a
    decision. The real decision is not whether to have a version 5.4 or
    not, it is all about solving the Unicode problem. The current effort
    has obviously stalled. We need to figure out how to get development
    back on track in a way that people can get on board. We knew the
    Unicode effort was hugely ambitious the way we approached it. There are
    other ways.
    So I think Lukas and others are right, let's move the PHP 6 trunk to a
    branch since we are still going to need a bunch of code from it and move
    development to trunk and start exploring lighter and more approachable
    ways to attack Unicode. We have a few already. Enhanced mbstring and
    ext/intl. Let's see some good ideas around that and work on those in
    trunk. Other features necessarily need to play along with these in the
    same branch. I refuse to go down the path of a 5.4 branch and a
    separate Unicode branch again.
    The main focus here needs to be to get everyone working in the same branch.
    -Rasmus
    March 11 2010

    View Slide

  26. March 11 2010
    PHP6

    View Slide

  27. PHP 6 trunk
    PHP 6 branch PHP 5.4 trunk

    View Slide

  28. The PHP 5.4 era sparked
    the PHP Renaissance

    View Slide

  29. How did we end up with PHP 7?

    View Slide

  30. Controversy

    View Slide

  31. PHP Next Generation

    View Slide

  32. To JIT or not to JIT?

    View Slide

  33. PHPNG vs HHVM

    View Slide

  34. Tackling
    misinformation

    View Slide

  35. When we aren't looking for pictures of kittens on the internet, internals developers are
    nearly always looking for ways to improve PHP, a few developers have a focus on performance.
    Over the last year, some research into the possibility of introducing JIT compilation
    capabilities to PHP has been conducted.
    During this research, the realization was made that in order to achieve optimal performance
    from PHP, some internal API's should be changed.
    This necessitated the birth of the phpng branch, initially authored by Dmitry Stogov, Xinchen
    Hui, and Nikita Popov. This branch does not include JIT capabilities, but rather seeks to
    solve those problems that prohibit the current, and any future implementation of a JIT
    capable executor achieving optimal performance by improving memory usage and cleaning up some
    core API's.
    By making these improvements, the phpng branch gives us a considerable performance gain in
    real world applications, for example a 20% increase in throughput for Wordpress. The door may
    well now be open for a JIT capable compiler that can perform as we expect, but it's necessary
    to say that these changes stand strong on their own, without requiring a JIT capable compiler
    in the future to validate them.
    The name "Next Generation" was optimistically presumptuous; in reality phpng is an internal
    project that we are working on, it is not a production ready branch that anyone should
    deploy, or judge as they would a release of PHP.
    The work on phpng, the doors it opens, the conversations it has started, the collaboration it
    is inspiring, are all worth getting excited about. But, we need to stay grounded, honest, and
    open; and say that there is much work to do in order to make the "Next Generation" a reality,
    this is only the start.
    May 27th 2014 by Joe Watkins

    View Slide

  36. Backlash

    View Slide

  37. Hi,
    Joe made a post about the introduction on phpng, what it is, and what it
    isn't.
    Some people (myself included) didn't liked that post for various reasons
    (some says it is opiniated, some doesn't like the tone and the wording,
    others feel that it is too early to made official announcement about phpng)..
    There were a couple of iteration on improving the text, but it is still not
    up to our standards imo:
    http://git.php.net/?p=web/php.git;a=history;f=archive/entries/2014-05-27-1.xml
    ;
    It is already on hackernews and reddit, so while some people suggested, I
    think it would be a bad move to just remove it.
    Would like to know what do you guys think about the best step, I see the
    following possible options:
    - keep it as is
    - remove it
    - rewrite it to be more formal and factual(only talk about what it is
    atm. not what it could be in the future).
    - create a post explaining that this post is controversional among the
    core-devs, so it is reflects more of the authors opinion than the projects
    official view on the topic.
    I'm mostly interested on the opinion of the core devs, but others also
    welcome to reply.
    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu
    May 28th 2014

    View Slide

  38. Hackernews
    Reddit
    Blogs
    Social media

    View Slide

  39. Sparked progress on the
    next major version of PHP

    View Slide

  40. Controversy

    View Slide

  41. PHP 6 or PHP 7?
    Or even PHP 5.7?

    View Slide

  42. Decision was made: PHP 7

    View Slide

  43. PHP 7 is all about
    performance

    View Slide

  44. Healthy competition
    with HHVM

    View Slide

  45. All this
    controversy

    View Slide

  46. Old boys club
    vs
    Young guns

    View Slide

  47. Balance between PHP as
    a easy-to-use scripting
    language and a mature
    future-proof
    programming language

    View Slide

  48. New language
    features

    View Slide

  49. ✓Scalar type hints
    ✓Return type declarations
    ✓Anonymous classes
    ✓Closure::call()
    ✓Generator delegation
    ✓Generator return expressions
    ✓Null coalesce operator
    ✓Space ship operator
    ✓Throwables
    ✓Uniform variable syntax
    ✓…
    PHP 7 new language features

    View Slide

  50. Controversy

    View Slide

  51. Loosely typed
    vs
    Strict types

    View Slide

  52. Compromise

    View Slide

  53. declare(strict_types=1);

    View Slide

  54. /**
    * Scalar type declarations
    */
    //declare(strict_types=1);
    function add(int $a, int $b) {
    return $a + $b;
    }
    var_dump(add(1,2));
    var_dump(add("1","2"));

    View Slide

  55. Fatal error: Uncaught TypeError: Argument 1 passed to
    add() must be of the type integer, string given
    TypeError: Argument 1 passed to add() must be of the type
    integer, string given

    View Slide

  56. /**
    * Return type declarations
    */
    //declare(strict_types=1);
    function add(int $a, int $b): int{
    return (string)($a + $b);
    }
    var_dump(add(1,2));

    View Slide

  57. Fatal error: Uncaught TypeError: Return value of add() must
    be of the type integer, string returned
    TypeError: Return value of add() must be of the type
    integer, string returned

    View Slide

  58. Type Errors

    View Slide

  59. Throwables
    Errors Exceptions
    Type error

    View Slide

  60. //Type error
    try {
    function add(int $a, int $b):int {
    return $a + $b;
    }
    echo add(array(), array());
    } catch (TypeError $t) {
    echo "Type error: ".$t->getMessage().PHP_EOL;
    }

    View Slide

  61. Throwables

    View Slide

  62. /**
    * Throwable interface
    */
    //Error as Throwable
    try {
    sqdf();
    } catch (Throwable $t) {
    echo "Throwable: ".$t->getMessage().PHP_EOL;
    }
    //Exception as Throwable
    try {
    throw new Exception("Bla");
    } catch (Throwable $t) {
    echo "Throwable: ".$t->getMessage().PHP_EOL;
    }

    View Slide

  63. /**
    * Throwable interface
    */
    //Exception
    try {
    throw new Exception("Bla");
    } catch (Error $e) {
    echo "Error: ".$e->getMessage().PHP_EOL;
    } catch (Exception $e) {
    echo "Exception: ".$e->getMessage().PHP_EOL;
    }
    //Type error
    try {
    function add(int $a, int $b):int {
    return $a + $b;
    }
    echo add(array(), array());
    } catch (TypeError $t) {
    echo "Type error: ".$t->getMessage().PHP_EOL;
    }

    View Slide

  64. Anonymous classes

    View Slide

  65. /**
    * Anonymous classes
    */
    $foo = new class {
    public function foo() {
    return "bar";
    }
    };
    var_dump($foo,$foo->foo());

    View Slide

  66. Generators
    Since
    PHP 5.5
    Iterator
    shorthand
    Less
    overhead

    View Slide

  67. Yield

    View Slide

  68. function gen_one_to_three() {
    for ($i = 1; $i <= 3; $i++) {
    yield $i;
    }
    }
    $generator = gen_one_to_three();
    foreach ($generator as $value) {
    echo "$value\n";
    }

    View Slide

  69. /**
    * Generator delegation
    */
    function gen()
    {
    yield 1;
    yield 2;
    yield from gen2();
    }
    function gen2()
    {
    yield 3;
    yield 4;
    }
    foreach (gen() as $val)
    {
    echo $val, PHP_EOL;
    }

    View Slide

  70. /**
    * Generator return expressions
    */
    $gen = (function() {
    yield 1;
    yield 2;
    return 3;
    })();
    foreach ($gen as $val) {
    echo $val, PHP_EOL;
    }
    echo $gen->getReturn(), PHP_EOL;

    View Slide

  71. Space ship operator

    View Slide

  72. /**
    * Space ship operator
    */
    $array = [
    "1 <=> 1" => 1 <=> 1,
    "1 <=> 2" =>1 <=> 2,
    "2 <=> 1" => 2 <=> 1
    ];
    var_dump($array);
    array(3) {
    '1 <=> 1' =>
    int(0)
    '1 <=> 2' =>
    int(-1)
    '2 <=> 1' =>
    int(1)
    }
    Output

    View Slide

  73. Null coalesce
    operator

    View Slide

  74. /**
    * Null coalesce operator
    */
    $array = ['foo'=>'bar'];
    //PHP5 style
    $message = isset($array['foo']) ? $array['foo'] : 'not set';
    echo $message.PHP_EOL;
    //PHP7 style
    $message = $array['foo'] ?? 'not set';
    echo $message.PHP_EOL;

    View Slide

  75. Closure::call()

    View Slide

  76. /**
    * Closure::call()
    */
    class Foo
    {
    private $foo = 'bar';
    }
    $getFooCallback = function() {
    return $this->foo;
    };
    //PHP5 style
    $binding = $getFooCallback->bindTo(new Foo,'Foo');
    echo $binding().PHP_EOL;
    //PHP7 style
    echo $getFooCallback->call(new Foo).PHP_EOL;

    View Slide

  77. Uniform variable
    syntax

    View Slide

  78. Uniform variable syntax

    View Slide

  79. PHP 7 is fast

    View Slide

  80. View Slide

  81. Speed depends on
    the your code and
    your case

    View Slide

  82. Up to 300% speed
    increase with lower
    memory usage

    View Slide

  83. Don't believe the
    banchmarks though

    View Slide

  84. Why is PHP 7 so fast,
    even without the JIT?

    View Slide

  85. ZVAL
    Internal
    variable
    representation
    Loosely
    typed variables in
    a strictly typed
    language

    View Slide

  86. Typedef struct _zval_struct {
    union {
    long lval;
    double dval;
    struct {
    char *val;
    int len;
    } str;
    HashTable *ht;
    struct {
    zend_object_handle handle;
    zend_object_handlers *handlers;
    } obj;
    } value;
    zend_uint refcount;
    zend_uchar type;
    zend_uchar is_ref;
    } zval;
    Typedef struct _zval_struct {
    union {
    long lval;
    double dval;
    zend_refcounted *counted;
    zend_string *str;
    zend_array *arr;
    zend_object *obj;
    zend_resource *res;
    zend_reference *ref;
    void *ptr;
    } value;
    union {
    struct {
    zend_uchar type;
    zend_uchar flags;
    };
    zend_uint type_info;
    };
    zend_uint reserved;
    } zval;
    PHP 5 vs PHP 7

    View Slide

  87. Remove pointers
    (to pointers)
    to ZVALs

    View Slide

  88. Direct use of ZVAL
    in most cases

    View Slide

  89. From heap to stack

    View Slide

  90. Less refcounting
    Less garbage collection

    View Slide

  91. New memory
    manager
    Less CPU
    time
    Less
    memory
    overhead

    View Slide

  92. A lot of small
    optimizations

    View Slide

  93. View Slide

  94. https://blog.feryn.eu
    https://talks.feryn.eu
    https://youtube.com/thijsferyn
    https://soundcloud.com/thijsferyn
    https://twitter.com/thijsferyn
    http://itunes.feryn.eu

    View Slide