Home
About
Search
🌐
English Română
  • PHP 5.6 awesomeness!

    Citește postarea în română

    Feb 22, 2014
    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.

Claudiu Perșoiu

Programming, technoloy and more
Read More

Recent Posts

  • Moving away from Wordpress
  • Custom path for Composer cache
  • Magento2 and the ugly truth
  • A bit of PHP, Go, FFI and holiday spirit
  • How to make use of the Xiaomi Air Conditioning Companion in Home Assistant in only 20 easy steps!
  • How I use Magento2 on my local with Docker and Docker Compose
  • About passion, programming and heating systems
  • The Books for Zend Certified Engineer 2017

PHP 49 MISCELLANEOUS 44 JAVASCRIPT 13 MAGENTO 7 MYSQL 7 BROWSERS 6 DESIGN-PATTERNS 5 LINUX-UNIX 2 WEB-STUFF 2 GO 1

PHP 35 JAVASCRIPT 14 PHP5.3 11 MAGENTO 7 PHP6 7 MYSQL 6 PHP5.4 6 ZCE 6 CERTIFICARE 5 CERTIFICATION 5 CLOSURES 4 DESIGN-PATTERNS 4 HACK 4 ANDROID 3
3D1 ADOBE-AIR2 ANDROID3 ANONYMOUS-FUNCTIONS3 BOOK1 BROWSER2 CARTE1 CERTIFICARE5 CERTIFICATION5 CERTIFIED1 CERTIFIED-DEVELOPER1 CHALLENGE1 CHM1 CLASS1 CLI2 CLOSURES4 CODE-QUALITY1 CODEIGNITER3 COLLECTIONS1 COMPOSER1 CSS1 DEBUG1 DESIGN-PATTERNS4 DEVELOPER1 DEVELOPMENT-TIME1 DOCKER1 DOCKER-COMPOSE1 DOUGLAS-CROCKFORD2 ELEPHPANT2 FACEBOOK2 FFI1 FINALLY1 FIREFOX3 GAMES1 GENERATOR1 GO1 GOOGLE1 GOOGLE-CHROME1 GOOGLE-MAPS1 HACK4 HOMEASSISTANT1 HTML2 HTML-HELP-WORKSHOP1 HTML51 HUG1 HUGO1 INFORMATION_SCHEMA1 INI1 INTERNET-EXPLORER3 IPV41 IPV61 ITERATOR2 JAVASCRIPT14 JQUERY1 LAMBDA1 LINUX1 MAGENTO7 MAGENTO22 MAP1 MINESWEEPER1 MOTIVATION1 MYSQL6 NGINX1 NODE.JS2 NOSQL1 OBSERVER3 OBSERVER-PATTERN1 OOP1 OPERA1 OPTIMIZATION1 ORACLE1 PAGESPEED1 PAIR1 PARSE_INI_FILE1 PHONEGAP2 PHP35 PHP-ELEPHANT2 PHP-FOR-ANDROID1 PHP-GTK1 PHP42 PHP53 PHP5.311 PHP5.46 PHP5.53 PHP5.61 PHP67 PHP7.41 PROGRAMMING1 REVIEW1 ROMANIAN-STEMMER2 SAFARY1 SCALAR-TYPE-HINTING1 SCHEME1 SET1 SHOPPING-CART-PRICE-RULE1 SINGLETON1 SOAP1 SPL2 SQLITE1 SSH1 STACK-TRACE1 STDERR1 STDIN1 STDOUT1 SUN1 SYMFONY2 TEST-TO-SPEECH1 TITANIUM2 TRAITS1 TTS1 UBUNTU1 UNICODE2 UTF-82 VECTOR1 WEBKIT1 WINBINDER1 WINDOWS1 WORDPRESS1 YAHOO3 YAHOO-MAPS1 YAHOO-OPEN-HACK1 YSLOW1 YUI1 ZCE6 ZCE5.31 ZEND3 ZEND-FRAMEWORK3
[A~Z][0~9]

Copyright © 2008 - 2021 CLAUDIU PERȘOIU'S BLOG. All Rights Reserved