Over a million developers have joined DZone.

Playing with the new PHP5.4 features

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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


From http://gonzalo123.wordpress.com/2011/11/28/playing-with-the-new-php5-4-features/

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}