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.


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

$a = [1, 2, 3];

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) {

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


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

23 thoughts on “Playing with the new PHP5.4 features

    1. Ohh, didnt see the
      [ ] 🙂

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

      hi([new Human(), ‘hello’, ‘world’]);

      1. 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)

        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)

      1. 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();

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

        would be a function which returns a class

        function Human() {
        return new Foo();

      3. 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.

  1. 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);}

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

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

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

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