New in PHP 5.3

PHP is approaching another important release, version 5.3. The release candidate is already available, if you’re interested in trying things out early. I thought I’d take a moment however, and highlight four really interesting new features we can expect in this new release.

1) Namespaces

Adding namespaces solves, in a stroke, a great many conflicts that developers tend to run into with PHP. For example, if I write a database layer class called db and YOU write a database layer called db, and I try to include another library that you’ve written that references your database layer, I’m boned. Since they’re both declared globally, this attempt at code re-use fails.

However, if I write a database layer and I decide to put it in the “danhulton” namespace, and you put yours in the “anotherlibrary” namespace, we’re fine. I refer to “danhulton\db” and you refer to “anotherlibrary\db” and all is right in the world.

Sadly, the namespace delimiter they’ve chosen is the backslash, which to my eyes just looks ODD, and also can run you into issues if you ever have to put your namespace in a string (with backslash being the escape character). Still, this is a net win for writing shareable PHP code.

2) Anonymous Functions

I got into the habit of using anonymous functions liberally in javascript, and have come to sorely miss them in PHP. Sure, you can emulate them with create_function(), but the syntax is awkward and the resource cost prohibitive. As of PHP 5.3, we’ll be able do do something a little like this:

$escaped = array_map(function($value) {
    return "'" . $value . "'";
}, array('sql', 'parameters', 'that', 'need', 'escaping'));

I’m very much looking forward to the cleaner code this will allow me to produce.

3) Ternary (?:) Operator

The ternary operator already exists in PHP, and I use it frequently. It allows you to do something like this:

$colour = ($balance > 0) ? 'green' : 'red';

Or, if you need to ensure a value is set before using it, you can write:

$entries = isset($entries) ? $entries : array();

In PHP 5.3, you’ll be able to simply write:

$entries = $entries ?: array();

Which means you can also write:

foreach ($entries ?: array() as $entry) {
    // some stuff

This change is excellent because, again, it leads to cleaner code.

4) Native MySQL library driver

This should be a huge win for many applications which value efficiency. (Although, isn’t that all of them?) The MySQL native driver is a full replacement for the currently-used driver written my MySQL AB. It is more efficient in terms of memory usage (storing all rows only once instead of twice), and should be just as processor-friendly, if not more. Further, there are a host of performance statistics calls that should ensure that performance debugging is much easier, and they include a function I wish had been there from the beginning: mysqli_fetch_all(). No more having to write a big while() loop with mysql_fetch_assoc(), this baby will take care of you in one line.

Now, since I’ve switched to the Kohana framework, my database functions are a lot friendlier already, so the latter function won’t specifically change how I code. But the decreased memory costs, especially for queries that return many rows, will be fantastic.


PHP 5.3 is going to be an excellent addition to the language, one I can’t wait for. I expect a fair amount of grumbling as we get adjusted to all the new features, and I’m rather curious to see what Kohana and other frameworks decide to do with regard to support (ideally for Kohana, IMHO, is keep KO 2.3 compatable with PHP 5.2.9, but require PHP 5.3 for KO 2.4 and 3.0). However, in the long run, we’ll have the ability to write cleaner, clearer, and less code, and that will be a fantastic advantage.

Rage-quit support for fish shell
Code faster with simple Sublime Text improvements
Gulp.js – an AMAZING build system!
  • Geoff



    To me,

    foreach ($entries ?: array() as $entries) {
    // some stuff

    Looks needlessly confusing. It’s trying to do too much on one line, when putting the “make sure entries is set” and “iterate through contents of entries” instructions would be better kept on separate lines.

    ( Also, I don’t realy know PHP, but why are the values in your array being referred to by the name of the array, “entries”, and not “value” or even “entry”? )

  • Dan Hulton

    Dan HultonDan Hulton


    To your first point, yes, it’s definitely confusing if it’s only encountered occasionally. But if it’s a common pattern throughout the code, you grow used to it. A lot of people have the “what the heck, foreign object!” response to the ternary operator in the first place, but that doesn’t mean it’s a poor idiom. Ditto for method chaining.

    As to your second comment, that was just a goof. It’s too bad WordPress doesn’t have a PHP syntax-checker built right in.