PHP 5.4 new features

PHP have introduced lot of interesting features in last few major versions. Some are obviously inspired from latest programming trends & from other languages. But these features make php more elegant and promising to code. Now I will discuss some of features in php version 5.4 I liked in following sections.

  • Short Array Syntax
  • Binary Numbers
  • Class::{expr}() and (new foo)->bar()) syntax.
  • Traits
  • Array Dereferencing

Short Array Syntax

This is my personal favorite feature. PHP have long history for define array through php function like this

[code lang=php]
$single_level = array(1, 2, "a" => "bcd");
$multi_level = array('first' => array(1, 2), 'second' => array(3,4), 'third' => array('3f' => array(5,6), '3s' => array(7,8)));
[/code]

Not we have short syntax which eliminate the using array keyword using more mainstream operator for this.

[code lang=php]
$single_level = [1, 2, "a" => "bcd"];
$multi_level = ['first' => [1, 2], 'second' => [3,4], 'third' => ['3f' => [5,6], '3s' => [7,8]]];
[/code]

Binary Numbers

PHP have support for octal and hexdecimal numbers for long time. But same privilege is not available for binary numbers. functions like decbin & bindec is only deal with string representation for binary numbers. Now we have binary number support they can be defined like.

[code lang=php]
$bin = 0b11011;
[/code]

See 0b prefix which define for binary number. We already have prefix 0 and 0x for octal and hexadecimal repectively.

Class::{expr}()

If we need to call dynamic method for a class in earlier versions we something similar code.

[code lang=php]
class A
{
public static function functA()
{
echo "call A".PHP_EOL;
}
public static function functB()
{
echo "call B".PHP_EOL;
}
}

$vars = array('A, 'B');
foreach($vars as $var)
{
$c = 'funct'.$var;
A::$c();
}
[/code]

Now in 5.4 version instead of variable for method name we can code like

(new foo)->bar())

If we need to call a method for a class in earlier versions we something similar code.

[code lang=php]
class A
{
public $var = 10;
public function methd()
{
echo $this->var.PHP_EOL;
}
}

$a = new A();
$a->methd();
[/code]

Now in 5.4 version instead of variable for method name we can code like this

[code lang=php]
(new A)->methd();
[/code]

Like previous example this one too eliminate the use of variable for just one method calling.

Traits

I write a separate blog for introduce trait at https://kuldeep15.wordpress.com/2015/03/17/php-traits/

Array De-referencing

PHP provide a way to avoid creating variable when we need to access a particular member of array, specially when we accessing data dirtecly returned from function.

In previous versions :

[code lang=php]
function foo
{
return array("a" => "First", "b" => "Second");
}

$a = foo();
echo $a[1];
[/code]

Using array de-referencing

[code lang=php]
function foo
{
return array("a" => "First", "b" => "Second");
}

echo foo()[1];
[/code]

You can also use it for some special cases like below.

[code lang=php]
$str = "A,B,C,D,E";
echo explode(',',$str)[2];
[/code]

In further articles I will explore interesting features from 5.5 & 5.6