Looking at New Features in PHP 5.4

PHP 5.4 was released at the start of March and heralds a key new feature which I have long bemoaned the lack of: traits.

Traits (see PHP’s documentation and Wikipedia’s description) allow programmers to define a set of functions which can be shared among different classes in ways that normal inheritance cannot do.

A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

I’ve banged my head against the desk a number of times trying to work around the lack of traits in PHP and I hope PHP 5.4 makes its way into mainstream Linux distributions a lot quicker that 5.3 did.

There are also a number of other interesting features which look really cool:

  • Simpler array dereferencing: $secondElement = getArray()[1];
  • A shortened array syntax: $a = [ "foo" => "bar", "bar" => "foo" ];
  • A built in web server (for development) which could be really interesting – see here.