PHP 5.6 awesomeness!

Citește postarea în română

Share on:

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:

1const INT_CONST = 1;
2
3const STRING_CONST = "string";
4
5const HEREDOC_CONST = <<<'EOT'
6nowdoc
7EOT;

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

1define('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:

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

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

1define('FLAG', true);
2
3const 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:

 1function variableParameters () {
 2     $params = func_get_args();
 3     foreach ($params as $parameter) {
 4          echo $parameter . PHP_EOL;
 5     }
 6}
 7
 8variableParameters(1);
 9variableParameters(1, 2);
10variableParameters(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:

1function variableParameters (...$params) {
2     foreach ($params as $parameter) {
3          echo $parameter . PHP_EOL;
4     }
5}

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:

1function variableParameters () {
2     $params = func_get_args();
3     foreach ($params as $parameter) {
4          echo $parameter . PHP_EOL;
5     }
6}

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:

1$params = ['param1', 'param2', 'param3'];
2
3call_user_func_array("variableParameters", $params);

The result will be:

1param1
2param2
3param3

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:

1variadicParameters(...$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:

1function twoParams($a, $b) {
2     echo $a . $b . PHP_EOL;
3}
4$params = ["Hello", "PHP 5.6!"];
5
6twoParams(...$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:

1function variableParameters (...$params) {
2     foreach ($params as $parameter) {
3          echo $parameter . PHP_EOL;
4     }
5}
6
7variableParamerers(...["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.