New Features in PHP 5.4

PHP 5.4 has been released, bringing with it a host of new features and fixes. TechRepublic has arrested many enhancements that Web developers should take into account in the new version.

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.

Also Read: Messaging Apps for iPhone

Some early evidence has shown that Zend Framework runs 21% faster and uses 23% less memory under 5.4, for example, Drupal and uses 50% less memory and runs about 7% faster.
new features in php 5.4


Traits is probably the most talked about new features in PHP 5.4 – think of them as compiler-assisted copy-and-paste. Traits are a feature of Scala as well. Other languages may refer to them as “mixins” – or they may not name them at all but have an extended interface mechanism that allows the interface to contain an actual implementation of its methods.
In contrast to mixins, traits in PHP include explicit conflict resolution if multiple traits implement the same methods.

trait Singleton {
public static function getInstance() { … }

class A {
use Singleton;
// …

class B extends ArrayObject {
use Singleton;
// …

// Singleton method is now available for both classes

See for many more examples including the conflict resolution syntax, method precedence, visibility, and support for constants and properties in traits. Also, for more of the theory behind the concept, you can read Nathan Schärli’s dissertation, “Traits: Composing Classes from Behavioral Building Blocks.”

Short Array Syntax

A simple, but very popular syntax addition:

$a = [1, 2, 3];
$b = [‘foo’ => ‘orange’, ‘bar’ => ‘apple’];

That is, you now no longer need to use the ‘array’ keyword to define an array.

Function Array De-referencing

Another oft-requested syntax addition. Function calls that return an array can now be de-referenced directly:

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

Instance Method Call

Regarding the array dereference function, now you can call a method on the object instance. And like in previous versions, you can, of course, still calls to methods in the chain, so you can now write code like this:

class foo {
public $x = 1;

public function getX() {
return $this->x;
public function setX($val) {
$this->x = $val;
return $this;

$X = (new foo)->setX(20)->getX();
echo $X; // 20

Although, unless your builder is doing something useful, since the instantiated object is discarded maybe you should use a static method call here instead. When combined with the syntax of short arrays and array references that function can write really complicated code:

class foo extends ArrayObject {
public function __construct($arr) {

echo (new foo( [1, [4, 5], 3] ))[1][0];

At first glance, you can tell what the outcome? Here is a two-dimensional array is passed to the constructor, which only returns the array. Then select the first element of the second dimension, so this would output “4″.

Also Read: JDBC Connectivity with MySql

Objects as Functions

There is a new magic method called “__invoke” which can be used like this:

class MoneyObject {
private $value;
function __construct($val) {
$this->value = $val;
function __invoke() {
return sprintf(‘$%.2f’,$this->value);

$Money = new MoneyObject(11.02/5*13);
echo $Money(); // Outputs: $28.65

Leave a Reply

Your email address will not be published. Required fields are marked *