PHP 5.5.0 Released

PHP reached a new milestone 3 days ago. And as always it’s quite exciting news. Some of the new features are already outlined by Evert Pot’s post which you can find here. And all of this of course can be found on the wiki and in the change log.

I’ll just outline some of the new and exiting features, deprecated notices and removed functions.

Simplified password hashing API

Hashing passwords with md5() and sha1() just isn’t enough anymore. So a new, secure and easy way to hash passwords has been added. And besides hashing includes some other useful functions.

$password The password string to hash

$algorithm The hashing algorithm to use of which two are available at the moment

PASSWORD_DEFAULT which uses bcrypt
PASSWORD_BCRYPT which uses blowfish

$options makes it possible to add a salt or set the cost for the hashing algorithm

Hash password with default algorithmpassword_hash()
$password = 'test-password';
$hash = password_hash($password, PASSWORD_DEFAULT);
var_dump($hash);
string(60) "$2y$10$qGv1q5nT4F7HCtKSPPME2usrdJRcRpk9lEUMQsE8mqyDIy3fbJ4I."
Hash password with BLOWFISH algorithmpassword_hash()
$hash = password_hash($password, PASSWORD_BCRYPT);
var_dump($hash);
string(60) "$2y$10$XtpNO/tFjtkq4u3ghcpqXeSwbHZxDQDTXRHfWBnZsmowUVl/MQys2"
Hash password with BLOWFISH algorithm and optionspassword_hash()
$salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM); 
$hash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 14, "salt" => $salt));
var_dump($hash);
string(60) "$2y$14$6ZtnYJ0CyqCUx.vJu3MZEuUGgIN.ryxMa0Yh8BnCrbBDVnd3Me30i"

Verify if a hash and password match. Return true if they do and false if they don’t

Verify hashpassword_verify()
$hash = password_hash($password, PASSWORD_DEFAULT);

if (password_verify($password, $hash)) {
  echo 'Password is correct';
} else {
  echo 'Password is incorrect';
}
Password is correct

Get information about a valid hash created with password_hash(). The function returns an array with the use d algorithm and options

Retrieve $hash informationpassword_get_info()
$info = password_get_info($hash);
array(3) {
  ["algo"]=>
  int(1)
  ["algoName"]=>
  string(6) "bcrypt"
  ["options"]=>
  array(1) {
    ["cost"]=>
    int(10)
  }
}

Check if the supplied hash was generated by the provided algorithm and options. This might come in handy when the hash needs to be updated

Check if a $hash needs to be rehashedpassword_needs_rehash()
if (password_needs_rehash($hash, PASSWORD_BCRYPT, array('cost' => 8))) {
  // Update the password hash
}

Support for constant array/string dereferencing

If you work with objects you might have worked with object dereferencing. This Is used to chain method calls. Or the so called fluent interfaces

Dereference object call
$obj->method()->returnObjMethod();

Now the same is possible for arrays and strings

Dereference strings / array
echo array(1, 2, 3)[0]; //output 1
echo "foobar"[2]; //output o
echo "foobar"["foo"][0] // output f
 
echo [1,3,4][2]; //output 4

Class Name Resolution As Scalar Via “class” Keyword

An easy way to get the full class name

Class name resolution via ::class
namespace Vodka\Crypt;

Class HashBuilder {}

use Vodka\Crypt\HashBuilder;
         
var_dump(HashBuilder::class);
string(23) "Vodka\Crypt\HashBuilder"

Support for using empty() on the result of function calls and other expressions

Normally empty() and isset() could only be used on variables. In 5.5 it’s possible to test expression and return values from functions

5.3

Call empty() with closure
var_dump( empty(function() {}) );

PHP Parse error: syntax error, unexpected T_FUNCTION in

Call empty() with function return value
        
function foo() {}
var_dump(empty(foo()));

PHP Fatal error: Can’t use function return value in write context in

5.5

Call empty() with function return value
function foo($val) { 
  return $val; 
}

var_dump( empty(foo([])) );
var_dump( empty(foo(true)) );
bool(true)
bool(false)

Support for list in foreach

List has been added for foreach loops. Great for eliminating unused variables.

Support for list in foreach
$messages = array(
  array('id' => 1, 'body' => 'test-1', 'code' => 12),
  array('id' => 2, 'body' => 'test-2', 'code' => 12),
  array('id' => 3, 'body' => 'test-3', 'code' => 10)
);

// Before
foreach ($messages as $message) {
  list($id, $body) = $message;
}
         
// After
foreach ($messages as list($id, $body)) {}

Zend Opcache extension and enable building it by default

My short post about Zend Optimizer+ in February this year. Kind of slipped my mind. And i was somehow under the impression APC would be integrated. But this of course has to be Zend Optimizer+. Finally an opcode cacher available be default. And configurable from php.ini

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=0
opcache.enable_cli=0

; The OPcache shared memory storage size.
opcache.memory_consumption=64

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=4

; Max files in OPCode cache, use a number between 200 and 100000.
opcache.max_accelerated_files=2000

; The maximum percentage of “wasted” memory until a restart is scheduled.
opcache.max_wasted_percentage=5

; Append current working dir to script name
opcache.use_cwd=1

How often a file should be validated
opcache.revalidate_freq=2

; Enables or disables file search in include_path optimization
opcache.revalidate_path=0

; Drop all PHPDoc comments
opcache.save_comments=1

array_column function which returns a column in a multidimensional array

Fetching a column from a multi-dimensional array is now possible with a single function call.

Fetch a column from a multi-dimensional arrayarray_column()
$nestedArray = array(
  array('id' => 1, 'body' => 'test-1', 'code' => 12),
  array('id' => 2, 'body' => 'test-2', 'code' => 12),
  array('id' => 3, 'body' => 'test-3', 'code' => 10)
);
$columns = array_column($nestedArray, 'code');
print_r($columns);
Array
(
  [0] => 12
  [1] => 12
  [2] => 10
)

Or fetch status code indexed by id

Fetch a column from a multi-dimensional array index by another fieldarray_column()
$columns = array_column($nestedArray, 'code', 'id');
print_r($columns);
Array
(
  [145] => 12
  [20098] => 12
  [34] => 10
)

deprecated

The following mcrypt functions have been deprecated mcrypt_ecb(), mcrypt_cbc(), mcrypt_cfb(), mcrypt_ofb() and will now throw E_DEPRECATED.

The mysql extension has finally been deprecated, and deprecation warnings will be generated when connections are established to databases via mysql_connect(), mysql_pconnect()

use MySQLi or PDO_MySQL extensions instead.

removed

The following (not so useful) functions have been removed from the core php_logo_guid(), php_egg_logo_guid(), php_real_logo_guid(), zend_logo_guid(). And support for the ancient operating systems Windows XP and 2003 has been dropped!

Install 5.5 on Ubuntu (experimental)

If you want to experience the new version first hand and you work on Ubuntu. You can add the experimental PPA and give it a shot.

sudo add-apt-repository ppa:ondrej/php5-experimental
sudo apt-get update
sudo apt-get install php5
comments powered by Disqus