Claudiu Persoiu

Blog-ul lui Claudiu Persoiu


PHP 5.6 awesomeness!

with 2 comments

The year has just started and a new PHP version is preparing for release. At the time when I’m writing this blog, it is in alpha 2. It can be downloaded from here: snaps.php.net.

There is a series of interesting features, but today I’ll approach only 3 of them: constant scalar expressions, variadic functions and arguments unpacking.

Constant scalar expressions

This first feature is also the only one that didn’t have an equivalent before this version. And, when I say equivalent, I mean only for classes.

Until now, constants defined using the keyword “const” didn’t allow any type of expression, only a scalar value could be used, ex:

const INT_CONST = 1;

const STRING_CONST = "string";

const HEREDOC_CONST = <<<'EOT'
nowdoc
EOT;

Of course, there is also the more interesting version, which allows setting constants with dynamic values:

define('DYN_CONST', $variable);

The bad part is that this type of constants are not available for a class, because for a class only const can be used.

This means that now we can use:

const ONE = 1;
const TWO = ONE * 2;
const STR = "string of one and two :" . ONE . TWO;

And if it’s not enough, there is always:

define('FLAG', true);

const EXP = FLAG ? ONE : TWO;

Basically, we can set dynamically a value for a class constant! For instance, you can set like this in a constant the environment type for the user, dev or prod.

Variadic functions

This is the feature that I like the most, even though there is an equivalent for it in the older versions.

Currently, if you want to create a function with a variable number of parameters, you can:

function variableParameters () {
     $params = func_get_args();
     foreach ($params as $parameter) {
          echo $parameter . PHP_EOL;
     }
}

variableParameters(1);
variableParameters(1, 2);
variableParameters(1, 2, 3);

There are several problems, more or less obvious, with an approach like this:
1. From the function signature it is not clear that it accepts more parameters, on the contrary, it seems that it doesn’t take any;
2. func_get_args can be called anywhere in the function. If it’s on the first line, the purpose will be obvious, if it’s on line 20, it’s hard to spot and may generate confusion.

An alternative is to add a number of parameters, but this is not a good indication of the varying behavior.

And here is the new approach:

function variableParameters (...$params) {
     foreach ($params as $parameter) {
          echo $parameter . PHP_EOL;
     }
}

In this new version, it’s clear what is happening with the function parameters. You don’t have to figure out if and how many parameters the function can take, it is clear that the number varies!

Usually, when I’m looking for a function, I look for the definition in the IDE before checking out the documentation, especially when there is little or no documentation.

Arguments unpacking

The problem that is approached here is: there is a function with multiple parameters and they are sent in a dynamic fashion.

As I was saying previously, there are alternative ways to solve this problem. Let’s use the function from the previous example:

function variableParameters () {
     $params = func_get_args();
     foreach ($params as $parameter) {
          echo $parameter . PHP_EOL;
     }
}

And let’s say there is a variable number of parameters, moreover they are stored in an array. We don’t have a lot of options to pass them:

$params = ['param1', 'param2', 'param3'];

call_user_func_array("variableParameters", $params);

The result will be:

param1
param2
param3

In PHP 5.6 we can use those 3 points, similarly to the way they were used in the example from “Variadic functions”, but the other way around. Instead of the parameters that are received by the functions, they are the parameters being sent to it:

variadicParameters(...$params);

Maybe for this type of example it is not very clear, but if there is a fixed number of parameters for the function and a fixed number of parameters that are sent:

function twoParams($a, $b) {
     echo $a . $b . PHP_EOL;
}
$params = ["Hello", "PHP 5.6!"];

twoParams(...$params);

Even though it’s not something that couldn’t be done in the previous versions, now it’s more elegant.

And to conclude, the two examples:

function variableParameters (...$params) {
     foreach ($params as $parameter) {
          echo $parameter . PHP_EOL;
     }
}

variableParamerers(...["cool", "parameter", "unpacking"]);

I think with these small changes, PHP is making the transition from “the PHP way” to “the best way”, taking from other languages.

Written by Claudiu Persoiu

22 February 2014 at 10:17 PM

Posted in PHP

Tagged with ,

2 Responses to 'PHP 5.6 awesomeness!'

Subscribe to comments with RSS or TrackBack to 'PHP 5.6 awesomeness!'.

  1. Salut,

    O mica remarca vreau sa fac: daca vorbim de “the best way”, atunci nu cred ca functiile de 20 de randuri intra in discutii…

    Banu Ioan

    23 February 2014 at 2:55 PM

  2. Salut,

    Ma refer la paradigme de programare, de exemplu func_get_args nu este o abordare tocmai fericita. Faptul ca aceste abordari se schimba, chiar daca functionalitatea nu este foarte diferita este un pas spre “the best way”.

    Claudiu Persoiu

    23 February 2014 at 4:55 PM

Leave a Reply