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

Working with Request objects in PHP (II). Back to the past

In one of my last post “Working with request objects in PHP”, I wrote a simple library to handle request objects. According that post let’s do a bit of history of PHP. In the early years of PHP (PHP3 – PHP4) one of the cool features of PHP was the “variable injection” inside our projects with register_globals=on. If you had the following a url:


Your script had magically a variable called $parameter1 with the value “Hi”. This feature has horrible security problems, if our user can inject variables in our scripts, especially with a loose typing program language like PHP. Because of that we all swap from those injected variables to get the value from $_POST and $_GET superglobals. In fact “injected variables” are disabled long time ago within PHP configuration.

Nowadays we don’t use $_POST $_GET superglobals directly. We need to filter the input. Because of that I wrote RequestObject library. Now we’re going to back to the past and allow the use of injected variables, but filtered.

RequestObject has now an extra public function called getFilteredParameters. This function returns an array with all already filtered input parameters. So if we use “extract” function we can create variables for each input parameters, but with the filtered values:

class Request extends RequestObject
    /** @cast string */
    public $param1;
     * @cast string
     * @default default value
    public $param2;

$request = new Request();

echo "param1: <br/>";
echo "<br/>";

echo "param2: <br/>";
echo "<br/>";

Full library available on github here