PHP 5.4 Released

So the other day, Rasmus tweeted that PHP 5.4 was fully released (following several release candidates):


There are some great additions, the highlights (other than a huge increase in speed, apparently) being square bracket notation for arrays, array dereferencing and the ability to use traits.

I’m quite excited (sadly) about the use of square brackets to initialise an array, and to be able to code up their contents in this way:


// PHP 5.3
$arr = array();
$arr2 = array('a','b','c');
$arr3 = array('a' => 2, 'b' => 4, 'c' => 8);
$arr4 = array(array('abc','def'), array(2,4,8), 16);

// PHP 5.4
$arr = [];
$arr2 = ['a','b','c'];
$arr3 = ['a' => 2, 'b' => 4, 'c' => 8];
$arr4 = [['abc','def'], [2,4,8], 16]];

This is similar to the way we do lists in Python.

Array dereferencing means we can now access particular elements within an array upon creating it:


function makeArray() {
    return [1,2,3];

// The PHP 5.3 way
$arr = makeArray();
echo $arr[2];

// The PHP 5.4 way
echo makeArray()[2]; // returns 3

This works the same way with Objects:

There’s now something called Traits, which is a concept brought in to PHP 5.4:

This allows a trait to be reused by any objects which refer to it in this way. It’s to save on copy and pasting blocks of code. The compiler now does that for us!

Also, up to and including PHP 5.3 we could attempt to echo an array without a notice given, but the word ‘Array’ would be echoed instead of any of the array’s content. Now in PHP 5.4 a notice is given:

Although it does still echo the word ‘Array’.

Here’s a great video (the keynote at PHPUK Conference in London) of Rasmus talking about the PHP project from the beginning, and about PHP 5.4:

Tags: php, python