What’s new in PHP 5.4

If we compare the most popular web scripting languages / programming languages, then undoubtedly PHP is the most widely used language. The LAMP stack has been strong for now almost 15 years and with every release / update in PHP is making it even popular and more stable and feature rich.

In 2009 PHP team released PHP 5.3 with lots of major changes and additions like namespace, late static binding, mysqlnd (MySQL Native Driver), closures, GC (Garbage Collection), improved performance on windows and many more. After 3 years of release of PHP 5.3 this year we have the even improved PHP 5.4. Many things has been changed and made improve by this 3 years gap.

Now you must be thinking what the changes are in this new release. Well there has been a quite improvement with this version. Below are the few of the new features that you will see when you upgrade to PHP 5.4

Memory and Performance

We see quite a significant change and optimization in internal structures. Many of these items are made smaller and some of them even removed, which resulted in less consumption of memory and faster execution time. In some cases performance can be found to be improved even upto 30%! Here is a quick list of major changes…

  1. Optimization in various common code path
  2. $GLOBALS is added to Just-In-Time debugger (JIT)
  3. Faster access to constants by pre computed hash
  4. Empty array is faster and consumes less memory
  5. Run time caching of class, function and constans
  6. @ operator executes much faster

Traits

Traits is probably the mostly discussed about new features in PHP 5.4. In simple word trait is brought into feature to overcome the multiple inheritance problem in PHP. Though trait does not work the same way that multiple inheritance work, still it is a good alternate of multiple inheritance. Trait can have static method, static property, can use other trait, can have abstract methods and so on. To know more about traits, its precedence and conflict resolution syntax see http://www.php.net/traits

New Syntax – Short Array

Addition of a simple, yet most popular syntax

$foo = [1, 2, 3, 4];
$bar = ["a"=>1, "b"=>2, "c"=>3];

Array De-referencing

Function calls that returns as can now be de-referenced directly.

function foo{
    return [1, 2, 3, 4]; //Returning an array
}

echo foo()[1]; //Prints 2

Instance Method Call

Now you can call a method directly on object instantiation along with chain method calls like the previous version.

class foo{
    public $var = 1;
    function set($val){
        $this->var = $val;
    }

    function get(){
        return $this->var;
    }
}

echo (new foo)->set(2)->get(); //Prints 2

Closure Binding

We have seen closures in PHP 5.3, but it has been a tweaked a bit on how it interacts with object.

class foo{
  private $var;
  function __construct($val){
    $this->var = $val;
  }

  public function getProperty(){
    return function(){
      echo $this->$var};
    }
  }

$o = new foo("Hello World!");
$f = $o->getProperty();
$f(); //Prints Hello World!

Please note that closures in PHP uses early binding, which means that the varialbles inside the closures will have the value they had when the closure was defined. Closures may also be rebound. See the example below…

$o = new foo("Hello");
$p = new foo("World");
$f = $o->getProperty();
$f(); //Prints Hello
$f = $f->bindTo($p);
$f() //Prints World;

The magic method __invoke

There is a new magic method named __invoke, which can be used as follows…

class foo{
  private $var;
  function __construct($val){
    $this->var = $val;
  }

  function __invoke(){
    return $this->var;
  }

}

$o = new foo("Hello");
echo $o(); //Prints Hello

Built-in Web Server Support

The CLI can work as a web server. This built-in web server is a really tiny one and you cannot compare it with Apache or IIS nor it can be used in a production environment. It has been implemented to quickly test your PHP pages on your computer without installing a web server separately. The web server can be started with the following command

php –S localhost:8080
PHP 5.4.0 Development Server started at Sun Apr 15 13:27:09 2012
Listening on localhost:8080
Document root is C:\PHP5
Press Ctrl-C to quit.

Binary Notation

Along with the support of Octal and Hexadecimal notation this new version of PHP supports binary notation as well.

$a = 0b00110011;

Improved Error Message

The error messages are now more meaningful and thus it makes even easier to understand the cause of the error.

Array to String conversion notice

If you noticed in earlier version of PHP it used to print Array() if you tried to echo an array() unwillingly. In this latest release you will get a warning if you do so.

$a = [1, 2, 3];
echo $a;

Note: Array to string conversion in test.php on line 2;

Removal of deprecated functions

Finally lots of functions that were marked as deprecated has been removed from this new release. To know the removed functions see php.net

The short echo tag

Regardless of what has been set for short_open_tag now PHP supports the short echo tag. So the following syntax will always work.

<?= $x ?>

There are more than 100 of changes in this new release. You should have a look at php.net for a complete list of changes. The upgrade from PHP 5.3 to 5.4 is fairly easy and smooth, but before you do that it is always a good idea to know the changes in complete detail.