What To Expect In PHP 5.4

What To Expect In PHP 5.4

With Release Candidate 7 available for download PHP is moving moving towards the final release of the much-anticipated (almost 3 years since PHP 5.3) 5.4 version. Here are a some things you’ll be able to enjoy or rant about.

Traits

The most talked, excited and disappointed about feature. Traits are set to solve the lack of multiple inheritance in PHP. Some developers are happy others are furious, others don’t know what to think. A lot is being said. In the end it’s just a convenience, as everything else, and runs down to ones and zeroes on the heap.

Array dereferencing

Returning arrays from functions and using them straight away will be possible.

function get_link_list() {
  return array('home' => '/', 'about' => '/about');
}
echo get_link_list()['home'];

Of course, there’s no point in dereferencing static arrays, but if your function builds up arrays dynamically then it’s utterly convenient.

Class member access on instantiation

PHP 5.4. allows for access to anonymous instance members like so:

class Library {
  public function get_book($book) {
    return ...; // Book
  }
}

$book = new Library()->get_book(1038);

Pointless if the object is used more than once. The instantiated object is forever lost unless the member returns $this. The constructor does this, why bother? Static methods can be used as well if instantiation is not crucial. Maybe method chaining for different instantiation paths? Can’t see it being put to good use, it saves only 1 line of code in the end.

$library = new Library;
$book = $library->get_book();

Indirect method calls

Method callbacks can be called and typehinted at.

$my_function = array('MyClass', 'my_method');
$my_function();

Instead of calling call_user_func, just call the method callback. A ‘callable’ typehint will be available.

Built-in HTTP server

One of the most interesting additions – a development HTTP server that can be launched for testing purposes in a CLI environment. Nothing fancy but highly useful.

Closure object support and binding

Closure objects are back and closure objects can be rebound to a different object and class scope.

Check out the RFC and this in-depth look at Closure object binding, which provides excellent explanations and examples.

Short array syntax

More syntactic sugar. Arrays can be defined as [value, key => value], instead of using the array() construct. A minor convenience.

Variable class expressions

Class methods can be called inline like so MyClass->{expression}(), where the expression can be any valid PHP expression, functions, mathematical expressions, etc.

Binary notation

0b1011011 will be possible in PHP 5.4. A new hex2bin function will also be available.

Limit stack frames in tracebacks

Debug backtracing can be limited in regards to the number of stack frames it returns. Highly useful.

header_register_callback

A registered callback with header_register_callback will be called right before headers are output.


In general, lots of syntactic sugar is added, with more hype than value, and quite a number of excellent features, hundreds of bugfixes and performance improvement, which should mean that it’s going to be faster.

If you feel adventerous, grab the latest release candidate over at http://qa.php.net/. Don’t forget to make test and submit the test results to QA, more information here.

What do you think? Are you excited about any feature in particular? How much time will the new syntactic sugar cubes save you?