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)
3. 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;
}
4. 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';
}
5. PASSWORD HASHING API
A super easy library that uses underlying crypt library.
password_get_info()
password_hash()
password_needs_rehash()
password_verify()
6. 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;
}
7. 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
}
8. 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;
}
13. 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
14. 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
15. 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!
16. WHAT TO DO ABOUT DEPRECATED APC
MODULE?
APC User Cache is in the works:
APC minus the opcode cache!
github.com/krakjoe/apcu
17. 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
18. 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?