Playing with the new PHP5.4 features


PHP5.4 it’s close and it’s time to start playing with the new cool features. I’ve created a new Virtual Machine to play with the new features available within PHP5.4. I wrote a post with the most exciting features (at least for me) when I saw the feature list in the alpha version. Now the Release Candidate is with us, so it’s the time of start playing with them. I also discover really cool features that I pass over in my first review

Let’s start:

Class member access on instantiation.

Cool!

class Human
{
    function __construct($name)
    {
        $this->name = $name;
    }

    public function hello()
    {
        return "Hi " . $this->name;
    }
}

// old style
$human = new Human("Gonzalo");
echo $human->hello();

// new cool style
echo (new Human("Gonzalo"))->hello();

Short array syntax
Yeah!

$a = [1, 2, 3];
print_r($a);

Support for Class::{expr}() syntax

foreach ([new Human("Gonzalo"), new Human("Peter")] as $human) {
    echo $human->{'hello'}();
}

Indirect method call through array

$f = [new Human("Gonzalo"), 'hello'];
echo $f();

Callable typehint

function hi(callable $f) {
    $f();
}

hi([new Human("Gonzalo"), 'hello']);

Traits

trait FlyMutant {
    public function fly() {
        return 'I can fly!';
    }
}

class Mutant extends Human {
    use FlyMutant;
}

$mutant = new Mutant("Storm");
echo $mutant->fly();

Array dereferencing support

function data() {
    return ['name' => 'Gonzalo', 'surname' => 'Ayuso'];
}

echo data()['name'];

IDEs don’t support (yet) those features. That’s means IDEs will mark those new features as syntax errors but I hope that they will support them soon.
More info here

About these ads

About Gonzalo Ayuso

Web Architect specialized in Open Source technologies. PHP, Python, JQuery, Dojo, PostgreSQL, CouchDB and node.js but always learning.

Posted on November 28, 2011, in php, Technology. Bookmark the permalink. 23 Comments.

  1. $a = [1, 2, 3];
    Finally!

    What would the return from “Callable typehint” be?
    Gonzalohello or Gonzalo ERROR ?

    • Ohh, didnt see the
      [ ] :-)

      What would
      function hi(callable $f) { $f(); }

      hi([new Human(), 'hello', 'world']);
      return?

      • I’m really excited with the sort syntax of arrays :)

        callable typehint allows us to call “callable” things (closures, function names or even indirect method call through array)

        Anyway
        hi([new Human(), 'hello', 'world']);
        it’s wrong

        array(new Human(), ‘hello’) is a callabel element now (indirect method call through array)

        Maybe you want to do

        function hi(callable $f, $name) { $f($name); }
        hi([new Human(), 'hello'], ‘world’);

        (I don’t have my VM with PHP5.4 so I cannot check it with a real instalation. I will check it later)

  2. Am I the only one who doesn’t like the “echo (new Human(“Gonzalo”))->hello();” syntax?

    • It would be much better if just
      echo new Human(‘foo’)->hello();
      worked

      • echo new Human(‘foo’)->hello();
        sounds good but it has conflicts with Human (function) and Human (class).

        The only viable solution is (new Human(‘foo’))->hello();

      • Well you have the
        echo new Human;
        echo new Human()->foo() = class
        echo Human::static = class
        echo Human() = function

        Human()->foobar
        would be a function which returns a class

        function Human() {
        return new Foo();
        }

      • It would still be ugly oneliner.

      • I understand It can be “ugly” but IMHO is a cool way to avoid write factory methods. Namespace separator also was ugly at the begininig but now I love it.

  3. Nice! I’ve been waiting a long time for “Array dereferencing support” in PHP (http://codeigniter.com/forums/viewthread/140798/). Love the short array syntax too, as well as any other feature that let us write more concise/terse code.

    When will be possible to assign deafult values as in JavasCript ($name = $name || ‘Gonzalo';)?

  4. Traits look useful. Performance data would be interesting.

  5. I’m still waiting for these:

  6. I’m still waiting for these:

    function is_closure($v) {return is_object($v) and is_callable($v);}

    function is_iteratable($v) {return is_array($v) or is_object($v) and !is_callable($v);}

  7. Very cool indeed. Traits and new arrays are really interesting:)

  8. this is very cool man, i waiting for this …

  9. $a = [1, 2, 3];
    print_r($a);

    Its throw Error
    Parse error: syntax error, unexpected ‘[‘ in D:\xampp\htdocs\joseph\worex\thephp\array\arrayDe.php on line 2

  1. Pingback: Quelques nouvelles fonctionnalités de PHP5.4 | Geekeries de Webdév

  2. Pingback: O que há de novo no PHP 5.4? Conheça as mudanças mais significativas. | PHPit

  3. Pingback: SCALE PHP BOF – The Links Listing « Sharon Lee Levy's Blog

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 972 other followers

%d bloggers like this: