PHP 5.4

Remember when I was complaining about the state of PHP? I think congratulations are in order, because it’s time to retract some of that criticism. Oh, not all of it, but this is a really promising release. Let’s run over the key features:

  1. Array dereferencing!
  2. Short array syntax!
  3. Better error messages!
  4. Callable typehint!
  5. Traits!
  6. Cooler dynamic method calling with classes
  7. <?= is always available
  8. Built in natural sorting

And a myriad of other bug fixes (although not the one I was looking for…), performance improvements (yay!), and a host of miscellaneous features I’d love to brag about for them (class member access on instantiation!), but we’ll stick to these for now.

Well what do you know…

So, those first four bullet points? Yep, they address 4 of the 7 things I was complaining about just last October. Happily, there’s more hope for PHP than I thought! Although the callable typehint RFC is only a small step in the right direction, I have more hope that the other open RFCs for return type hints and more parameter type hinting may some day be implemented. In the meantime, the array syntax improvements and clearer error handling are welcome features. Now I just need to wait 5ish years to use them in production!


Surprise! PHP is upgrading their object model again! No, you won’t have to rewrite all your code, fear not. So what are these traits? In short, they’re a way of having multiple inheritance without quite the headache of multiple inheritance. A concise description might be “interfaces that can have implemented methods”. If you’re really interested, you can look at the manual (or if theory is your thing, the RFC). For everyone else, I’ll walk through a brief example:

class mySuperClass {
    public function function1() { /*0*/ }

trait myTrait {
    public function function1($arg) { /*1*/ }
    protected static function function2($arg) { /*2*/ }

class myClass extends mySuperClass {
    use myTrait {
        function2 as public;
        function1 as traitFunction1;

$foo = new myClass();

There’s a lot going on here. First, we define a trait, which defines two functions: a public function function1, and a protected static function function2. Note that you can’t implement traits directly (ie “new myTrait();” will cause a runtime exception). So, we have to use it by invoking “use [traitName]”. So, myClass, since it extends mySuperClass, gets the functionality of function1, and since it uses myTrait, it gets a static function2 and … wait a minute. Function1 again. No problem! the line “function1 as traitFunction1” tells PHP, “In this class, function1, which is defined in myTrait, is actually going to be called traitFunction1”. Crisis averted! So now we can make a new instance of myClass, and call all the functions on it… wait a minute. function2 is protected! Well, almost. It’s protected by default, but the line “function2 as public” tells PHP, “no, for this particular class I need it to be public”.

You can also declare abstract function in traits, change visibility and rename a function, use multiple traits (with method renaming for conflict resolution, although there’s also a precedence system), declare static variables in trait methods, declare class variables for traits1, and define traits that use other traits. It’s not a small thing to wrap your head around, but it provides another nice way to avoid copy and paste, which is evil. Features to avoid it are always welcome!

Everything Else

I know, the rest of the features don’t seem that cool at first. But consider:

<?php print $foo; ?>


<?= $foo ?>

That’s almost HALF the characters every time you want to print something. Which is all the time. It’s not a new feature, but now that it’s available everywhere (5.4+ anyway), we can use it in libraries and APIs. And the new SORT_NATURAL and SORT_FLAG_CASE modifiers save you from writing whole sort implementations (and the built in sort is at least an order of magnitude faster than a pure PHP solution too!). Finally, as more of a “wait, this is new?” feature, there are a couple new ways to dynamically call static methods. The following syntaxes are now valid:

class Test {
    public static staticMethod() {
        print "Hello World!";

$foo = array("Test", "staticMethod");
$foo(); //calls "staticMethod" on class Test, which prints "Hello World!" 
$bar = "staticMethod";
Test::{$bar}(); //also prints "Hello World!"


And let’s not forget the dozens of other features and bug fixes you can read about in the release notes. PHP 5.4 addresses some key concerns myself and others have long held, and while the feature list may not be all I hoped for, it’s an encouraging step in the right direction. So what are you waiting for? Download it!

Seriously, go update your servers! Fight community stagnation!

1 The documentation isn’t really clear on this, but it does work.

About Bion

I'm a software developer at Modo Payments, a mobile payment provider. When I'm not hacking away the office, you I'm usually at home hacking on something else. Or practicing Aikido. Anyway, I just post things here that Google couldn't help me with, so maybe it'll help you in the future. Since you're reading this, I guess it worked :)
This entry was posted in Technology and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *