Share
Embed
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
Transcript
38 slides
-
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 -
WHAT'S THIS TALK ABOUT? History: PHP 6 New features in
PHP 7 Removed features Q & A Some Nucleus 'announcements' Raf e? -
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 -
CRON.WEEKLY Weekly newsletter with linux & open source news, tools,
tips & projects www.cronweekly.com -
SPECIAL SHOUT-OUT Big thanks to Tom Schuermans ( ) for
his help in the presentation & organisation! @tschuermans -
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 -
PHP 7: WHAT'S NEW? Scalar type declarations Return type declarations
null coalesce operator Spaceship operator Arrays in constant de ne()'s Anonymous classes -
PHP 7: WHAT'S NEW? (CONT'D) Unicode codepoint escape syntax &
IntlChar Filtered (whitelisted) unserialize() Grouped namespacing in use Session improvements -
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 -
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} -
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) -
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} -
'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 -
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' -
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]; }); -
ANONYMOUS CLASSES <?php $app = new Application; // 'new class'
creates anonymous class $app->setLogger(new class implements Logger { public function log(string $msg) { echo $msg; } }); -
ANONYMOUS FUNCTIONS (PHP >= 5.3) <?php $example = function ($text)
{ echo $text; }; $example('I want that PHP 7 elephpant!'); -
UNICODE <?php echo "\u{aa}"; // Output in PHP < 7
\u{aa} // Output in PHP >= 7 ª -
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"] ]); -
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}; -
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(); -
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" -
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 -
UNIFORM VARIABLE SYNTAX <?php // PHP < 7 $object->$array['item']; -->
$object->{$array['item']}; // PHP >= 7 $object->$array['item']; --> ${object->$array}['item']; -
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) -
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 -
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) -
PERFORMANCE OF PHP 7 IN ONE SLIDE 6x faster than
PHP 5.5 1.6 faster than PHP 5.6 Your mileage may vary -
SOME NUCLEUS ANNOUNCEMENTS 100gr of PHP 7 for everyone! (conveniently
packed in a 16GB USB disk with the PHP 7 source code on it) -
SOME NUCLEUS ANNOUNCEMENTS As of today, PHP 7 support on
our shared hosting! Let's merge this feature live! -
SOME NUCLEUS ANNOUNCEMENTS Free PHP 7 shared hosting for everyone
here! (give me your e-mail address, I'll hook you up)