PHP 5.6 is coming next week

By Brandon Savage on August 21, 2014

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.

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:

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?

Struggling with the principles of object oriented programming?

No matter what you do you struggle to make sense out of object oriented development. It seems difficult, nay, impossible to master. But there's hope! Get this FREE 30 page guide on SOLID, the world's best known acronym for object oriented applications!

Get your FREE guide right now!

* indicates required

Email Address: *

First Name:

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?

Design patterns shouldn't be hard to learn. What if you could master them? Introducing a brand-new book on design patterns, from the author of Mastering Object Oriented PHP. Find out what you've been missing with design patterns!

Search

Search for:

About

Brandon Savage has been a software developer since 2003. Ever since discovering that he could use software to automate routine tasks, he's been hooked. Brandon is passionate about perfecting the art of software development.

Brandon blogs, speaks and writes regularly about software development topics. Brandon works at Mozilla on the Socorro project, working daily to make Firefox better. Brandon is also an instrument rated private pilot and enjoys traveling.

Get Free Weekly Developer Tips!

Free! Get weekly tips and tricks for perfecting the art of software development delivered to your inbox! Plus, in your first week you’ll get Five Tips For Developing Awesome Software delivered, one tip per day. Sign up and get the first message in minutes:

* indicates required

Email Address: *

First Name:

Write Better Object Oriented PHP Today

Object oriented programming always leaves you with a headache. What if you could master it instead? Stop struggling with object oriented PHP. Mastering Object Oriented PHP is the answer!