Whenever a new version of PHP surfaces, it can take years for people to actually even want to consider updating. Why? The pain of existing code simply not working, spending time learning new syntax that you may probably never use and the many bugs or errors that may be introduced into a perfectly working existing system.

A nuisance.

However, lately a lot of hosts have upgraded their servers to support PHP 5.4, although some experienced some slight issues (a couple of examples). We've slowly been implementing more support for PHP 5.4 whilst providing a fail-safe mechanism for PHP 5.3 (which our CMS and all software is natively developed for).

Why move to PHP 5.4?

Memory and Performance Improvements; a number of internal structures have been made smaller or eliminated entirely, which has resulted in 20-50% memory savings in large PHP applications. And performance is up by 10-30% (heavily dependent on what your code is doing) through a number of optimizations including inlining various common code-paths, $GLOBALS has been added to the JIT, the ‘@’ operator is faster, run-time class/function/constant caches have been added, run-time string constants are now interned, constant access is faster through pre-computed hashes, empty arrays are quicker and consume less memory, unserialize() and FastCGI request handling is faster, plus many more memory and performance tweaks were made throughout the code.

Some tests against our CMS have shown that it loads and runs 37% faster and uses 29% less memory under 5.4, for example, and Drupal uses 50% less memory and runs about 7% faster. (Oracle, PHP 5.4)

PHP 5.4.0 significantly improves performance, memory footprint and fixes over 100 bugs. Notable deprecated/removed features include register_globalsmagic_quotes (about time) and safe_mode. Also worth mentioning is the fact that multibyte support is enabled by default and default_charset has been changed from ISO-8859-1 to UTF-8.

New Features & Improvements

Some key new features include traits, a shortened array syntax, a built-in webserver for testing purposes, use of $this in closures, class member access on instantiation, <?= is always available, along with much more!

Array Syntax and Derefrencing

Arrays can now be constructued using a similar approach as JSON/Javascript literals providing a shorthand method:

# The old way
$fruits = array('apples', 'oranges', 'bananas');
# Same as JSON literal array
$fruits = ['apples', 'oranges', 'bananas'];
# Associative array
$array = [
    'foo' => 'bar',
    'bar' => 'foo'

Obviously, it is important to outline that the old/existing method will still work. Moreover, dereferencing of array elements from the result of function and method calls is now supported. The following code will work from PHP 5.4:

function fruits() {
    return ['apple', 'banana', 'orange'];
echo fruits()[0]; // Outputs: apple


The best demonstration of traits is when multiple classes share the same functionality. A trait is a set of methods that are structurally similar to a class (but similar to interfaces and abstract classes, cannot be instantiated) that can enable developers to reuse common sets of methods freely in several independent classes. This overcomes to inherent single-inheritance PHP problem allowing classes to inherit and use multiple traits.

The best use of traits is demonstrated when multiple classes share the same functionality. For instance, a website may need to use the Facebook and Twitter APIs and require a common cURL wrapper function/method to handle any requests and process their responses. By using traits in this example, we don't have redundant/repeated code and so we make reusable code, and follow the DRY (Don’t Repeat Yourself) principle.

 * cURL wrapper trait
trait cURL {
    public function curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        return $output;
 * Twitter API Class 
class TwitterAPI {
    # Use the trait (inheritance)
    use cURL;
    public function get($url) {
        return json_decode($this->curl('http://api.twitter.com/'.$url));
 * Facebook API Class 
class FacebookAPI {
    # Use the trait (inheritance)
    use cURL;
    public function get($url) {
        return json_decode($this->curl('http://graph.facebook.com/'.$url));

We can abstract away from this to a much simpler example demonstrating multi-inheritance of a class:

# Trait A
trait A {
    public function outputA() {
        return 'A';
# Trait B
trait B {
    public function outputB() {
        return 'B';
# Our class we'll instantiate
class SimpleAB {
    # Use the trait (inheritance)
    use A, B;
    public function output() {
        echo $this->outputA().' + '.$this->outputB();
# Create object, output!
$simpleAB = new SimpleAB()->output();

The above example outputs "A + B"

Objects as Functions

PHP 5.4 implements a new magic method called “__invoke” which can be used like this:

class DateObject {
    private $timestamp;
    function __construct($time) {
        $this->timestamp = $time;
    function __invoke() {
        return date('d/M/Y', $this->timestamp); 
# Create the object using current timestamp
$date = new DateObject(time());
# Automatically calls '__invoke'
echo $date(); // Outputs: 30/01/2013

Improved JSON extension

Objects can now implement the JsonSerializable interface allowing objects to be serialised into JSON.

class OurObject implements JsonSerializable {
    private $value;
    function __construct($value) {
        $this->value = $value;
# Create object
$ourObject = new OurObject('1');
# Encode it
echo json_encode($ourObject);

JSON now also supports the option JSON_PRETTY_PRINT making the output of a JSON string will be displayed in a more human readable format

# Setup simple object
$object = new stdClass;
$object->name = "Lewis";
$object->surname = "Theobald";
$object->birthdate = array("month" => "July", "day" => 30);
# Encode
echo json_encode($obj);
echo json_encode($obj, JSON_PRETTY_PRINT);
/* Output, without formatting:
Output, with formatting:
    "name": "Lewis",
    "surname": "Theobald",
    "birthdate": {
        "month": "July",
        "day": 30
Ignite your brand, utilise user-generated content no matter where you or your audience are ›