PHP 7

PHP 7


This article in Spanish.

PHP is used by many programmers and companies for web development. Due to its popularity it is constantly evolving. The last PHP version to be released was 5.6 and with the public rejection of version 6 we are happy to talk about PHP 7. Released last December, PHP 7 has some new features I would like to talk about and also discuss whether the update is worth it for you.

This version adds new functionalities, changes to PHP’s own internal management, and fixes several bugs. However, there are a few features that are now obsolete generating some incompatibilities we have to consider before updating the version in production environments.

Why upgrade?

Many companies and developers have chosen PHP over other technologies because it is adaptable and easy to implement. The downfall is that PHP performance decreases when traffic in websites increases. For that reason Facebook (known to use PHP) created an interpreter to enhance said performance: the HipHop Virtual Machine (HHVM). The HHVM translates the executed PHP code into machine code, boosting times needed to perform operations.

The HHVM inspired the PHP community to make improvements in their internal engine which makes PHP 7 much faster than PHP 5.6 and remarkably reduced memory consumption. With these two major improvements we can have more concurrent users without having to buy new hardware.

What’s new?

Some of the relevant changes and new functions of PHP 7:

  • Changes to error and exception handling: fatal errors are now exceptions.

  • Consistent support for 64 bit Windows systems.

  • The spaceship operator <=>: this operator returns 0 if the operators are equal, 1 if the left operator is greater and -1 if right operator is greater. It looks like a spaceship, right?

       echo 1 <=> 1; // outputs 0
       echo 4 <=> 2; // outputs 1
       echo 3 <=> 7; // outputs -1
  • Scalar type declaration: scalar types are the ones that hold single values, with PHP 7 we can now define scalar types such as bool, string, int and float. There are two ways of doing this: the coercive way and the strict way. In case we want to use the latter we need to use per-file the new strict_types declare() directive. This directive needs to be on top of the PHP file. When the strict_types is 1, it means that the file is in strict checking type mode. By default, PHP files are in weak type-checking mode, that is the same as having strict_types=0.
    <?php
	  // strict mode           
             declare(strict_types=1);
             function sum(int ...$ints) {
               return array_sum($ints);
             }
             print(sum(2, '1', 4.1));
     ?>

In the example above an error will occur because the argument ‘1’ is not an integer. On the other hand, if we write print(sum(2, 1, 4)), the output will be int(7).

  • Return type declarations: used to specify the type of the value that will be returned by a function, we do that adding a colon and writing the type before the curly brackets.
	function sum(int $a, int $b) : int {
  • Null coalescing operator: there are times when we want to check a value and if that value is null then return a default, the coalescing operator allows us to do that.

	<?php
     // if the value of $_GET['user'] does not exists, returns 'nobody'
        $username = $_GET['user'] ?? 'nobody';
     // The above code is equivalent to:
        $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
     ?>
  • Anonymous classes: anonymous classes allows us to create classes without having to name them, they are helpful when we only use a class once in the execution time and thus we do not need to document them.

  • Group use declarations: classes functions and constants can be imported from the same namespace and grouped together, saving typing time.

   <?php
    // Pre PHP 7 code
       use some\namespace\ClassA;
       use some\namespace\ClassB;
       use some\namespace\ClassC as C;

   // PHP 7+ code
      use some\namespace\{ClassA, ClassB, ClassC as C};
    ?>

How to migrate?

To migrate to version 7 you need to check if you have incompatibilities in your code or in the libraries that your code uses; you could remove the dependences of those libraries and use something else. Another beneficial thing to do is to write tests, and, according to the results you get, determine the parts of your code that need to be rewritten.

Conclusion

At the moment PHP 5 version is the most widely used. Nevertheless, version 7 has been gaining more usage, recently w3Techs stated that version 7 has surpassed version 4 in this sense. The transition of sites with previous versions to version 7 may be slow, but it is definitely happening. Without a doubt, the upgrade to PHP 7 is worth.