PHP 5.4 – The Highlights

ElephpantsI haven’t blogged for ages, so here’s one about the impending release (ie. today) of PHP 5.4! I am just giving a brief introduction to a few features which I think make it a rather interesting release.

Speed

There is going to be a significant speed increase in PHP 5.4 (apparently more so than we got from 5.2-5.3). According to http://news.php.net/php.internals/57760 there is a 20 – 50% increase from 5.3!

Arrays

There are a couple of new features in arrays. The first of all is array creation. You can now create an array using the JavaScript style square-brackets syntax [], rather than having to call the array function.

//in php 5.3 or below
$firstArray = array();
$secondArray = array('foo', 'bar');
$thirdArray = array('foo' => 'fooval', 'bar' => 'barval');

//in php 5.4
$firstArray = [];
$secondArray = ['foo', 'bar'];
$thirdArray = ['foo' => 'fooval', 'bar' => 'barval'];

This will please us JavaScript developers. Also what will please us is the new array dereferencing ability. You’ve long been able to reference an array element from a returned array in JavaScript, but in PHP you’ve had to assign it to a variable and reference from the variable:

//in php 5.3 or below
$arrayVar = $myObj->returnArray();
echo $arrayVar['foo'];

//in php 5.4
echo $myObj->returnArray()['foo'];

So these give us programmers more ways to be lazy! 😀

Class Member Access on Instantiation

Also like JavaScript, PHP 5.4 allows you to call/access a method/property immediately after instantiating the object (without assigning it to a variable).

//in php 5.3 or below
$foo = new Foo();
echo $foo->bar();

//in php 5.4
echo (new Foo)->bar();

Be aware that this instantiates an object, calls the bar() method, and then throws the object away. Generally this is pretty wasteful and often a static method is a more efficient way of doing it. However, it is nice to have this feature.

Traits

And another way we can be lazy is we can use traits. Traits are described as “compiler assisted copy and paste”. In a trait you can include create methods and properties which can be reused in multiple classes. This is not the same as inheritance, which means that you can use multiple traits in a single class. A great example of a use of a trait is for a singleton class (whether or not you agree that singletons should be used at all) which I saw at the PHPUK Conference.

trait Singleton {
  protected static $_instance;

  public static function getInstance() {

    if(self::$_instance == null) {
      self::$_instance = new self();
    }

    return self::$_instance;
  }
}

//now our singleton class, let's call it DatabaseHandler

class DatabaseHandler {
  use Singleton;

  private function __construct() {
    //...
  }
  //...
}

//So now you can do:

$dbh = DatabaseHandler::getInstance();

So, as stated before it is a compiler assisted copy and paste, that means a method in a trait can use private methods in the object, and vice versa. You can use as many traits as you like in a single class (unlike inheritance), and you can even change the visibility and create aliases for trait properties and methods on a per class basis. This is incredibly flexible and allows for increased reuse of code.

Built in Web Server

Finally, PHP 5.4 comes with its very own web server, allowing you to start developing without having a server stack. It is started by executing the PHP CLI binary with the -S switch followed by server:port (eg. localhost:80). Simple as that! It uses your current working directory as the root directory and router can even be specified.

devmachine:project user$ php -S localhost:80

Note: Do not use this in a production environment! Ever!

More Information

I for one am very excited about these features, and really looking forward to teaching and using them in production. As this is a brief overview, I have only highlighted certain new features, for a more in-depth analysis:

  1. Davey Shafik – PHP 5.4: The New Bits, this is the talk that Davey gave at PHPUK2012 (recommended read)
  2. A far more complete overview on King Foo
  3. An overview article on webtutor.pl of PHP 5.4