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

$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)));

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

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

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.

$bin = 0b11011;

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.

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();
}

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.

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

$a = new A();
$a->methd();

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

(new A)->methd();

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 :

function foo
{
    return array("a" => "First", "b" => "Second");
} 

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

Using array de-referencing

function foo
{
    return array("a" => "First", "b" => "Second");
} 

echo foo()[1];

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

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

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

PHP Traits

Traits are a new phenomenon in languages which do not support multiple inheritance. PHP also not directly support multiple inheritance while interface can be used to simulate but due to their abstract behavior, they are less usefull when we need functionality from two or more parent class into child class and we want to through inheritance.

Here Traits are introduced. They do exactly what multiple inheritance do, And while take care of method collision.

See following example in PHP without trait.

class Phone
{
    public function ring()
    {
    }
    public function message()
    {
    }
} 

class Computer
{
    public function email()
    {
    }
    public function surf()
    {
    }
    public function chat()
    {
    }
} 

class Smartphone extends Phone
{

}

We can not inherit the functionality of Computer into Smartphone class here. As computer class is already defined and user in another world we can not convert it into interface. So we need to make Computer interface for Smartphone class need to reimplement which duplicate Computer class structure.

interface ComputerInterface
{
    public function email();
    public function surf();
    public function chat();
}

class SmartPhone  extends Phone implements ComputerInterface 
{
    public function email()
    {
        // Copy implement here from computer class
    }
    public function surf()
    {
        // Copy implement here from computer class
    }
    public function chat()
    {
        // Copy implement here from computer class
    }
}

As we see this is not efficient way some times. So we need to use design patterns to resolve this problem. But trait resolve this problem in language itself.

trait Phone
{
    public function ring()
    {
    }
    public function message()
    {
    }
    public function login()
    {
    }
    public function logout()
    {
    }
} 

trait Computer
{
    public function email()
    {
    }
    public function surf()
    {
    }
    public function chat()
    {
    }
    public function login()
    {
    }
    public function logout()
    {
    }
}

class SmartPhone
{
    use Phone, Computer{
        Phone::login insteadof Computer;
        Computer::logout insteadof Phone;
    }
}

$smart = new SmartPhone();
$smart->login();
$smart->email();
$smart->ring();
$smart->logout();

Here we also see how trait handle same method name collision. Their are more functionality similar to classes available in trait like inheritance and abstract methods.

See (http://php.net/manual/en/language.oop5.traits.php)