New features in PHP5.4 alpha1


We already have the php5.4 alpha ready for testing. This new release has a few features that I really like. The lack of those features gave me problems in the past. Because of that, I’m very excited waiting for the new stable release of PHP 5.4.
Now I’m going to list the new features I like. There’re more features (check the full list here) but those ones are the ones I’m really waiting for:

Added array dereferencing support

When we’re doing OO we can do things like that:

class A
{
    public function foo()
    {
        return new B();
    }
}

class B
{
    public function bar()
    {
        return "Hi";
    }
}

$a = new A();
echo $a->foo()->bar(); // Will output Hi

But if we output an array instead of an instance of class, we will need an extra variable to use it:

class A
{
    public function foo()
    {
        return array('name' => 'Gonzalo');
    }
}

$a = new A();
$foo = $a->foo();
echo $foo['name'];

Now with PHP 5.4 we’ll use:

$a = new A();
echo $a->foo()['name'];

Cool, isn’t it?

Added indirect method call through array

Now an array(‘class’, ‘method’) can be used to call a function. is_callable will return true.

That means we can do:

class Foo {
   public function bar($name) {
      echo "Hi, $name";
   }
}

$f = array('Foo','bar');
echo $f('Gonzalo'); // Hi, Gonzalo

More information here

Added closure $this support back

When we use closures in PHP5.3 and we want to use $this statement, we need to do some strange hacks to use it in the context of the callback. Things like $that = $this; and use($that). It works, but looks like an ugly hack. Now we can do:

class A {
  private $value = 1;
  function single_getter($name) {
    return function() use ($name) {
      return $this->$name;
    };
  }
}
class B {
  private $value = 2;
  function test() {  
    $a = new A;
    $single_getter = $a->single_getter('value');
    print $single_getter();
  }
}  

$b = new B;
$b->test();

PHP5.3 -> Fatal error: Using $this when not in object context
PHP5.4 -> the script will output “1”

More information here.

Added support for Traits

And finally we will enjoy of traits:
Here we can read a really good article about it:

With Traits we can share interfaces and avoid inheritance nightmares to reuse code. Clean and simple.

Conclusions

In my handle opinion those new features will help PHP to grow a little bit more and adapt itself to new years. It’s a pity we still don’t have a short way to define arrays and we still need to use array(1, 2, 3) instead of doing things like [1, 2, 3], but I hope this new feature will be available in future versions I’m not sure but I think it’s on the roadmap.

Advertisement

11 thoughts on “New features in PHP5.4 alpha1

  1. The ‘Added indirect method call through array’ feature seems like a potential debug headache to me.

    1. Yes. Probably this feature may be a potential headache especially for the IDEs (because of the loosely/weak typed). But I this feature really cool when we’re working developing frameworks, API servers and things like that. Another tool to our toolbox. That’s means another way available to solve problems. I like it 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.