New features in PHP 5.4

The next iteration of 5.4 looks like it’s more focused on cleaning up the language than features.

It appears many of the previously deprecated functions are now, as promised, removed completely. As this article is about features, here is a quick summary of the other changes:

  • Removed: break/continue $var syntax
  • Removed: register_globals, allow_call_time_pass_reference, and register_long_arrays ini options
  • Removed: session_is_registered(), session_registered(), and session_unregister()

This information is based on alpha/beta releases and is subject to change.

Onwards to the new features…

Traits support

PHP 5.4 includes traits which allow reusable groups of methods to be used from any class.

Here is an example:

trait someMethods

    public function doSomething() {}

    public function doSomethingElse() {}


We can then use the above trait in an class using the use statement:

class someClass extends someParent

    use someMethods;

    public function doSomethingLocaly() {}


More information about traits can be found in the manual:

Array dereferencing

As PHP has had object dereferencing for ages, this is something that is really long overdue and addresses the inconsistency between object and array return values.

A quick/common example of the existing object dereferencing:

class someClass

    private $_someVar;
    private $_someOtherVar;

    public function setSomeVar($value)
        $this->_someVar = $value;
        return $this;

    public function setSomeOtherVar($value)
        $this->_someOtherVar = $value;
        return $this;


$class = new someClass();

Now we can do the same with arrays, without the current need to assign the array return value to a variable before we can access it’s contents:

class someOtherClass

    public function getArray()
        return array(1, 2, 3, 4, 5);


$class = new someOtherClass();
$third = $class->getArray()[2];

This is a rather pointless example, but demonstrates the new functionality.


Leave a Reply