What's New in PHP 5.5
-
Upload
corey-ballou -
Category
Technology
-
view
4.802 -
download
2
description
Transcript of What's New in PHP 5.5
WHAT'S NEW IN PHP 5.5
· Corey Ballou @cballou
GENERATORSSupport for generators has been added via the yield keyword
range(0, 1000000) vs. user supplied function
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;}
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';}
PASSWORD HASHING APIA super easy library that uses underlying crypt library.
password_get_info()password_hash()password_needs_rehash()password_verify()
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;}
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}
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;}
FOREACH + LIST$array = [ [1, 2], [3, 4],];
foreach ($array as list($a, $b)) { echo "A: $a; B: $b\n";}
ARRAY_COLUMN() FUN$people = [ [ 'firstname' => 'John', 'lastname' => 'Doe' ], [ 'firstname' => 'Jane', 'lastname' => 'Doe' ],];
// contains [ 0 => 'John', 1 => 'Jane' ]$firstnames = array_column($people, 'firstname');
IMPROVEMENTS TO EMPTY()function always_false() { return false;}
if (empty(always_false())) { echo "Hello, world.";}
ARRAY AND STRING LITERAL DEREFERENCING// array dereferencingecho [1, 2, 3][0];
// string dereferencingecho 'PHP'[0];
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
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
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!
WHAT TO DO ABOUT DEPRECATED APCMODULE?
APC User Cache is in the works:
APC minus the opcode cache!
github.com/krakjoe/apcu
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
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?
QUESTIONS? COMMENTS?
Thanks for pretending to enjoy my banter!