What's New in PHP 5.5

19
WHAT'S NEW IN PHP 5.5 · Corey Ballou @cballou

description

Follow me on Twitter at http://www.twitter.com/cballou or checkout my startup at http://www.pop.co. In this presentation we will cover the best features and additions in PHP 5.5. You can look forward to the following: * Support for generators has been added via the yield keyword * Usage of the new finally keyword in try-catch blocks * An overview and examples of the new password hashing API * The foreach control structure now supports unpacking nested arrays into separate variables via the list() construct * empty() supports arbitrary expressions such as closures returning false * array and string literal dereferencing * The Zend Optimiser+ opcode cache (via the new OPcache extension)

Transcript of What's New in PHP 5.5

Page 1: What's New in PHP 5.5

WHAT'S NEW IN PHP 5.5

· Corey Ballou @cballou

Page 2: What's New in PHP 5.5

GENERATORSSupport for generators has been added via the yield keyword

range(0, 1000000) vs. user supplied function

Page 3: What's New in PHP 5.5

EXAMPLE USAGE OF YIELD KEYWORDfunction getLines($filepath) { $f = fopen($filepath, 'r'); try { while ($line = fgets($f)) { yield $line; } } finally { fclose($f); }}

foreach (getLines("file.txt") as $n => $line) { echo $line;}

Page 4: What's New in PHP 5.5

THE "FINALLY" KEYWORD IS FINALLY HEREExecute code ALWAYS.

try { throw new Exception('hello');} catch (Exception $e) { echo $e->getMessage();} finally { // this code will always be run echo ', world';}

Page 5: What's New in PHP 5.5

PASSWORD HASHING APIA super easy library that uses underlying crypt library.

password_get_info()password_hash()password_needs_rehash()password_verify()

Page 6: What's New in PHP 5.5

EXAMPLE OF REGISTER/LOGINfunction register($username, $password) { $hash = password_hash($password, PASSWORD_BCRYPT); // save hash to database}

function login($username, $password) { $hash = getHashFromDbByUsername($username); if (password_verify($password, $hash)) { // perform login (store session var) return true; } return false;}

Page 7: What's New in PHP 5.5

INCREASING PASSWORD SECURITYYou can optionally supply your own salt and algorithmic cost.

function register($username, $password) { $options = array('salt' => 'someRandomSalt', 'cost' => 12); $hash = password_hash($password, PASSWORD_BCRYPT, $options); // save hash to database}

Page 8: What's New in PHP 5.5

EXAMPLE OF UPGRADING YOUR HASHINGALGORITHM

function login($username, $password) { $hash = getHashFromDbByUsername($username); if (password_verify($password, $hash)) { // check if hash is in updated format if (password_needs_rehash($hash, PASSWORD_BCRYPT)) { // perform update $hash = password_hash($password, PASSWORD_BCRYPT); // save new hash to database }

// perform login (store session var) return true; } return false;}

Page 9: What's New in PHP 5.5

FOREACH + LIST$array = [ [1, 2], [3, 4],];

foreach ($array as list($a, $b)) { echo "A: $a; B: $b\n";}

Page 10: What's New in PHP 5.5

ARRAY_COLUMN() FUN$people = [ [ 'firstname' => 'John', 'lastname' => 'Doe' ], [ 'firstname' => 'Jane', 'lastname' => 'Doe' ],];

// contains [ 0 => 'John', 1 => 'Jane' ]$firstnames = array_column($people, 'firstname');

Page 11: What's New in PHP 5.5

IMPROVEMENTS TO EMPTY()function always_false() { return false;}

if (empty(always_false())) { echo "Hello, world.";}

Page 12: What's New in PHP 5.5

ARRAY AND STRING LITERAL DEREFERENCING// array dereferencingecho [1, 2, 3][0];

// string dereferencingecho 'PHP'[0];

Page 13: What's New in PHP 5.5

NOW LET'S REALLY GET CRAZY...function foo() { return array(1, 2, 3);}echo foo()[2]; // prints 3

$func = function() { return array('a', 'b', 'c'); };echo $func()[0]; // prints a

Page 14: What's New in PHP 5.5

ZEND OPTIMISER+ OPCACHE EXTENSIONNot a replacement for APC/memcache(d). No user cache!Available in PHP 5.4 via install.Source available: https://github.com/zend-dev/ZendOptimizerPlus$ php -vPHP 5.4.17RC1 (cli) (built: Jun 22 2013 19:27:26)Copyright (c) 1997-2013 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technologies

Page 15: What's New in PHP 5.5

SO, UH, WHAT IS AN OPCODE CACHE?Component designed to speed up performance of PHPwithout altering the app.Overrides PHP's default compiler callback by checking if acompiled intermediate-code version of the code isavailable in-memory.It skips compilation when it can!

Page 16: What's New in PHP 5.5

WHAT TO DO ABOUT DEPRECATED APCMODULE?

APC User Cache is in the works:

APC minus the opcode cache!

github.com/krakjoe/apcu

Page 17: What's New in PHP 5.5

BACKWARDS INCOMPATIBLE CHANGESWin XP/2003 support droppedself, parent and static are case insensitivepack() and unpack() made more compatible with perlhttp://www.php.net/manual/en/migration55.deprecated.php

Page 18: What's New in PHP 5.5

CREDITSphp.net - What has changed in PHP 5.5.xwiki.php.net - Integrating Zend Optimizer+ into the PHPdistributionschlueters.de - Features in PHP trunk: Arraydereferencingslideshare.net - What's new in PHP 5.5stackoverflow.com - How do you use bcrypt for hashingpasswords in PHP?

Page 19: What's New in PHP 5.5

QUESTIONS? COMMENTS?

Thanks for pretending to enjoy my banter!