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

More Decks by Thijs Feryn

Other Decks in Technology


  1. By Thijs Feryn What's new in

  2. Hi, I’m Thijs

  3. I’m @ThijsFeryn on Twitter

  4. I’m an Evangelist At

  5. I’m a at board member

  6. To know where PHP is heading, you have to know

    what happened in the past
  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
  8. We've been using PHP 5 for 11 years

  9. Why is there no PHP 6?

  10. Windows 10?

  11. PHP 6 did once exist

  12. But was never released

  13. A history lesson

  14. First ideas in 2005 by Andrei Zmievski at Yahoo

  15. PHP 6 was all about unicode support

  16. And some extra language features

  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
  18. Delays, delays, delays …

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

  20. The rest was eventually shipped off to PHP 5.4

  21. None
  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
  23. Controversy

  24. In 2010 the core team concluded that Unicode support for

    PHP was too hard to implement
  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
  26. March 11 2010 PHP6

  27. PHP 6 trunk PHP 6 branch PHP 5.4 trunk

  28. The PHP 5.4 era sparked the PHP Renaissance

  29. How did we end up with PHP 7?

  30. Controversy

  31. PHP Next Generation

  32. To JIT or not to JIT?

  33. PHPNG vs HHVM

  34. Tackling misinformation

  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
  36. Backlash

  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
  38. Hackernews Reddit Blogs Social media

  39. Sparked progress on the next major version of PHP

  40. Controversy

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

  42. Decision was made: PHP 7

  43. PHP 7 is all about performance

  44. Healthy competition with HHVM

  45. All this controversy

  46. Old boys club vs Young guns

  47. Balance between PHP as a easy-to-use scripting language and a

    mature future-proof programming language
  48. New language features

  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
  50. Controversy

  51. Loosely typed vs Strict types

  52. Compromise

  53. declare(strict_types=1);

  54. <?php /** * 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"));
  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
  56. <?php /** * Return type declarations */ //declare(strict_types=1); function add(int

    $a, int $b): int{ return (string)($a + $b); } var_dump(add(1,2));
  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
  58. Type Errors

  59. Throwables Errors Exceptions Type error

  60. <?php //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; }
  61. Throwables

  62. <?php /** * 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; }
  63. <?php /** * 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; }
  64. Anonymous classes

  65. <?php /** * Anonymous classes */ $foo = new class

    { public function foo() { return "bar"; } }; var_dump($foo,$foo->foo());
  66. Generators Since PHP 5.5 Iterator shorthand Less overhead

  67. Yield

  68. <?php 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"; }
  69. <?php /** * 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; }
  70. <?php /** * Generator return expressions */ $gen = (function()

    { yield 1; yield 2; return 3; })(); foreach ($gen as $val) { echo $val, PHP_EOL; } echo $gen->getReturn(), PHP_EOL;
  71. Space ship operator

  72. <?php /** * 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
  73. Null coalesce operator

  74. <?php /** * 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;
  75. Closure::call()

  76. <?php /** * 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;
  77. Uniform variable syntax

  78. Uniform variable syntax

  79. PHP 7 is fast

  80. None
  81. Speed depends on the your code and your case

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

  83. Don't believe the banchmarks though

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

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

    typed language
  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
  87. Remove pointers (to pointers) to ZVALs

  88. Direct use of ZVAL in most cases

  89. From heap to stack

  90. Less refcounting Less garbage collection

  91. New memory manager Less CPU time Less memory overhead

  92. A lot of small optimizations

  93. None
  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