Over a million developers have joined DZone.
Platinum Partner

New features in PHP 5.4 alpha 1

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

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;

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.


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.

The Web Dev Zone is brought to you in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


Published at DZone with permission of Gonzalo Ayuso , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}