Get your FREE 30 page Developing SOLID Applications guide!

PHP 5.6 is coming next week

According to PHP.net, PHP 5.6 will be released next Thursday. This is an incremental release of the PHP language, offering a few new features. Yet even though this release is relatively small in features, the features included are powerful and will change the way each one of us develops.

PHP 5.6 is easy to install, and works the same way that PHP 5.5 did. In other words, if you have a proven process for installing PHP 5.5 from source, that process can work for upgrading to PHP 5.6. There are a few backwards incompatible changes to watch out for, but for the most part, you won’t have any trouble upgrading.

So why should you upgrade? There are four features that make PHP 5.6 worth putting on your system.

Reuse of php://input

Previously, once you read the php://input stream, you couldn’t use it again. That’s no longer the case in PHP 5.6.

Why does this matter? There may be cases where you want to read the php://input stream in various parts of your application, but you previously couldn’t do that. The now non-global state of php://input allows you to make use of the stream, without having to worry about the implications on the rest of your application.

According to the PHP team, the work done to optimize this bit of the language also dramatically reduced the memory usage associated with POST request data, so that’s a win for everybody.

The ability to import functions and constants with namespaces

The introduction of namespaces in PHP 5.3 meant that we could avoid namespacing collisions and create classes and objects that lived in their own namespaced world. While we have always been able to use functions that lived in a particular namespace, it wasn’t possible to import the functions themselves directly (only classes could be imported).

For example, if you have a function in namespace Foo\, you would have to call it like so:

use Foo;

Foo\myFunc();

Now, it’s possible to import the function itself into the namespace, like so:


use function Foo\myFunc;

myFunc();

Variadic functions and argument unpacking

In PHP 5.6, we introduce a new bit of syntax: the ellipsis (…). This bit of syntax does double duty for different behaviors. The first one is called Variadic functions, which allows for the creation of functions with variable length argument syntax, and works similar to the *args of Python.


function concatString(...$bits) {
    foreach($bits as $bit) {
        echo $bit . " ";
    }
}

This will be useful for adding variable length parameter options to functions, as well as permitting different numbers of arguments in a method. While we could always do this to some degree with func_get_args(), now we can have both named and unnamed parameters in the same function.

We can also use the ellipsis syntax for argument unpacking. The idea here is that if you construct an array that matches the arguments of a function, you can pass that set of arguments in and have them unpacked. For example:


$array = [1, 2, 3];

function add($a, $b, $c) {
    return $a + $b + $c;
}

add(1, ...[2, 3]); // Returns 6
add(...[4, 5, 6]); // returns 15

It’s important to note, this will not work with named keys. In other words, if your array contains keys that you have given a name to, this will result in a fatal error. This was done to preserve forward compatibility with another RFC (why they weren’t merged at the same time or made dependent on each other I’ll never know).

PHP 5.6 slated for Thursday release

PHP 5.6 should be available next Thursday, barring any unforseen discoveries in the fourth release candidate, so get ready to upgrade!

Write better object oriented PHP today.

Object oriented programming always leaves you with a headache. What if you could master it instead?

Get the book now! »

Spudley wrote at 8/21/2014 11:27 am:

Looking forward to PHP 5.6. Some solid features there that will definitely be useful.

One thing: I notice that your argument unpacking example doesn’t use the ellipsis operator – I’m guessing that’s a typo?

Brandon Savage (@brandonsavage) wrote at 8/21/2014 11:32 am:

Yep, good catch. I’ll fix it now.

Finau wrote at 8/26/2014 10:21 pm:

Hey Brandon, thanks for the lecture today in Wellington.

Cheers

Leave a Comment

To control spam, all comments are moderated.