DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Playing with the new PHP5.4 features

Playing with the new PHP5.4 features

Gonzalo Ayuso user avatar by
Gonzalo Ayuso
·
Nov. 29, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
5.87K Views

Join the DZone community and get the full member experience.

Join For Free

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

 

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

Virtual Machine IT Syntax (programming languages) Data structure Pass (software) Release (agency) Machine Data Types POST (HTTP)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Building a Login Screen With React and Bootstrap
  • Flutter vs React Native. How to Cover All Mobile Platforms in 2022 With No Hassle
  • Suspicious Sortings in Unity, ASP.NET Core, and More
  • Message Queuing and the Database: Solving the Dual Write Problem

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo