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

What's new in PHP 7?

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
  2. WHAT'S THIS TALK ABOUT? History: PHP 6 New features in

    PHP 7 Removed features Q & A Some Nucleus 'announcements' Raf e?
  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
  4. SPECIAL SHOUT-OUT Big thanks to Tom Schuermans ( ) for

    his help in the presentation & organisation! @tschuermans
  5. 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
  6. PHP 7: WHAT'S NEW? Scalar type declarations Return type declarations

    null coalesce operator Spaceship operator Arrays in constant de ne()'s Anonymous classes
  7. PHP 7: WHAT'S NEW? (CONT'D) Unicode codepoint escape syntax &

    IntlChar Filtered (whitelisted) unserialize() Grouped namespacing in use Session improvements
  8. 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
  9. 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}
  10. 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)
  11. 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}
  12. '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
  13. NULL COALESCE OPERATOR <?php // 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'
  14. 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]; });
  15. ANONYMOUS CLASSES <?php $app = new Application; // 'new class'

    creates anonymous class $app->setLogger(new class implements Logger { public function log(string $msg) { echo $msg; } });
  16. ANONYMOUS FUNCTIONS (PHP >= 5.3) <?php $example = function ($text)

    { echo $text; }; $example('I want that PHP 7 elephpant!');
  17. UNICODE <?php echo "\u{aa}"; // Output in PHP < 7

    \u{aa} // Output in PHP >= 7 ª
  18. FILTERED / WHITELISTED UNSERIALIZE() <?php // 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"] ]);
  19. GROUPED USE DECLARATIONS <?php 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};
  20. 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();
  21. 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"
  22. 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
  23. UNIFORM VARIABLE SYNTAX <?php // PHP < 7 $object->$array['item']; -->

    $object->{$array['item']}; // PHP >= 7 $object->$array['item']; --> ${object->$array}['item'];
  24. DATE.TIMEZONE WARNINGS REMOVED default still UTC <?php // PHP <

    7 Strict Standards: date(): It is not safe to rely on the system timezone settings. .... // PHP >= 7 (nothing)
  25. 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
  26. 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)
  27. PERFORMANCE OF PHP 7 IN ONE SLIDE 6x faster than

    PHP 5.5 1.6 faster than PHP 5.6 Your mileage may vary
  28. SOME NUCLEUS ANNOUNCEMENTS 100gr of PHP 7 for everyone! (conveniently

    packed in a 16GB USB disk with the PHP 7 source code on it)
  29. SOME NUCLEUS ANNOUNCEMENTS As of today, PHP 7 support on

    our shared hosting! Let's merge this feature live!
  30. SOME NUCLEUS ANNOUNCEMENTS Free PHP 7 shared hosting for everyone

    here! (give me your e-mail address, I'll hook you up)