WHAT'S NEW IN PHP 5.5
·Corey Ballou @cballou
GENERATORS
Support for generators has been added via the yield keyword
range(0, 1000000)vs. user supplied function
EXAMPLE USAGE OF YIELD KEYWORD
function 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 HERE
Execute code ALWAYS.
try {
throw new Exception('hello');
} catch (Exception $e) {
echo $e->getMessage();
} finally {
// this code will always be run
echo ', world';
}
PASSWORD HASHING API
A super easy library that uses underlying crypt library.
password_get_info()
password_hash()
password_needs_rehash()
password_verify()
EXAMPLE OF REGISTER/LOGIN
function 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 SECURITY
You 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 HASHING
ALGORITHM
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: $bn";
}
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 dereferencing
echo [1, 2, 3][0];
// string dereferencing
echo '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 EXTENSION
Not 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 -v
PHP 5.4.17RC1 (cli) (built: Jun 22 2013 19:27:26)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technolo
gies
SO, UH, WHAT IS AN OPCODE CACHE?
Component designed to speed up performance of PHP
without altering the app.
Overrides PHP's default compiler callback by checking if a
compiled intermediate-code version of the code is
available in-memory.
It skips compilation when it can!
WHAT TO DO ABOUT DEPRECATED APC
MODULE?
APC User Cache is in the works:
APC minus the opcode cache!
github.com/krakjoe/apcu
BACKWARDS INCOMPATIBLE CHANGES
Win XP/2003 support dropped
self, parent and static are case insensitive
pack()and unpack()made more compatible with perl
http://www.php.net/manual/en/migration55.deprecated.php
CREDITS
php.net - What has changed in PHP 5.5.x
wiki.php.net - Integrating Zend Optimizer+ into the PHP
distribution
schlueters.de - Features in PHP trunk: Array
dereferencing
slideshare.net - What's new in PHP 5.5
stackoverflow.com - How do you use bcrypt for hashing
passwords in PHP?
QUESTIONS? COMMENTS?
Thanks for pretending to enjoy my banter!

What's New in PHP 5.5

  • 1.
    WHAT'S NEW INPHP 5.5 ·Corey Ballou @cballou
  • 2.
    GENERATORS Support for generatorshas been added via the yield keyword range(0, 1000000)vs. user supplied function
  • 3.
    EXAMPLE USAGE OFYIELD KEYWORD function 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; }
  • 4.
    THE "FINALLY" KEYWORDIS FINALLY HERE Execute code ALWAYS. try { throw new Exception('hello'); } catch (Exception $e) { echo $e->getMessage(); } finally { // this code will always be run echo ', world'; }
  • 5.
    PASSWORD HASHING API Asuper easy library that uses underlying crypt library. password_get_info() password_hash() password_needs_rehash() password_verify()
  • 6.
    EXAMPLE OF REGISTER/LOGIN functionregister($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; }
  • 7.
    INCREASING PASSWORD SECURITY Youcan 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 }
  • 8.
    EXAMPLE OF UPGRADINGYOUR HASHING ALGORITHM 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; }
  • 9.
    FOREACH + LIST $array= [ [1, 2], [3, 4], ]; foreach ($array as list($a, $b)) { echo "A: $a; B: $bn"; }
  • 10.
    ARRAY_COLUMN()FUN $people = [ [ 'firstname'=> 'John', 'lastname' => 'Doe' ], [ 'firstname' => 'Jane', 'lastname' => 'Doe' ], ]; // contains [ 0 => 'John', 1 => 'Jane' ] $firstnames = array_column($people, 'firstname');
  • 11.
    IMPROVEMENTS TO EMPTY() functionalways_false() { return false; } if (empty(always_false())) { echo "Hello, world."; }
  • 12.
    ARRAY AND STRINGLITERAL DEREFERENCING // array dereferencing echo [1, 2, 3][0]; // string dereferencing echo 'PHP'[0];
  • 13.
    NOW LET'S REALLYGET 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
  • 14.
    ZEND OPTIMISER+ OPCACHEEXTENSION Not 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 -v PHP 5.4.17RC1 (cli) (built: Jun 22 2013 19:27:26) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technolo gies
  • 15.
    SO, UH, WHATIS AN OPCODE CACHE? Component designed to speed up performance of PHP without altering the app. Overrides PHP's default compiler callback by checking if a compiled intermediate-code version of the code is available in-memory. It skips compilation when it can!
  • 16.
    WHAT TO DOABOUT DEPRECATED APC MODULE? APC User Cache is in the works: APC minus the opcode cache! github.com/krakjoe/apcu
  • 17.
    BACKWARDS INCOMPATIBLE CHANGES WinXP/2003 support dropped self, parent and static are case insensitive pack()and unpack()made more compatible with perl http://www.php.net/manual/en/migration55.deprecated.php
  • 18.
    CREDITS php.net - Whathas changed in PHP 5.5.x wiki.php.net - Integrating Zend Optimizer+ into the PHP distribution schlueters.de - Features in PHP trunk: Array dereferencing slideshare.net - What's new in PHP 5.5 stackoverflow.com - How do you use bcrypt for hashing passwords in PHP?
  • 19.
    QUESTIONS? COMMENTS? Thanks forpretending to enjoy my banter!