PHP 7 Released with Many Improvements

PHP 7 Released with Many Improvements

December 3rd 2015 marked the official release of PHP 7, the new version of the popular programming language. The latest PHP version released was PHP 5 in 2004, more than 11 years ago. For this reason developers’ expectations are very high for this new version. In this article I will talk about the main improvements of PHP 7.

Introduction

To start, I’ll explain what happened with PHP 6. When PHP developers began planning for the version 6, they defined that a great improvement would be to implement full Unicode support in PHP strings. What happened was that this change brought so much trouble to the rest of PHP that developers decided to abandon the version 6 altogether. As there were already articles, books and groups that have used the version number 6, the final decision was to skip this version and go directly to version 7.

The PHP version 5 had several updates since it was launched back in 2004, the last one was version 5.6. In the meantime Facebook has been working in parallel, creating the PHP HipHop Compiler in 2010, then included it the HHVM (virtual machine to run compiled applications). In 2014 the Facebook released the programming languageHack, PHP-based and which can also be executed through HHVM. These initiatives were all parallel to the development of PHP, but assisted in the general direction of the language.

In this context the creation of the PHPNG (PHP Next Generation) was initiated, a Zend Engine refactoring. When the new engine was finished a great performance gain was achieved, so PHPNG became the basis for PHP 7.

Improvements in PHP 7

Next, an explanation of the major improvements of PHP 7 when compared to previous versions.

Performance

The most important improvement (and I’m sure most commented) is performance. According to the official website, PHP 7 is twice faster than the PHP 5.6. A considerable improvement. This was possible because PHPNG optimizes memory usage and adds just-in-time compilation (JIT). Below some graphics prepared by Zend performance team (to see the entire infographic, click here).

PHP7 Frameworks WP PHP7

New Operators

Two new operators were introduced in PHP 7.

Spacechip (<=>)

Spacechip operator, also known as combined comparison operator, allows making comparisons quickly. The notation is <=> and it works like this:

If $a is less than $z, the operation will return -1; If they are equal, the value will be 0; And if $a is greater than $z, the value will be 1. It’s a shorter way to write the following expression:

Null Coalesce (??)

The null coalesce operator allows you to check if a variable or array key exists and assign a default value if it does not exist. This was usually done with isset(), now you don’t have to.

It’s the equivalent to do:

Type declarations

The scalar types declarations can be defined in two ways: coercive (default) or strict. In the coercive mode, PHP will accept things like a integer variable be assigned with a float variable with no errors or warnings. For example:

The above code will run without problems. But if you want to force the strict mode you can use the following command:

From the moment that this declaration has been made (you need the statement in each file), if you assign a string to a float or int variable, as in the above code, you will get the following error:

Fatal error: Uncaught TypeError: Argument passed to add() 2 must be of the type integer, string given.

The only exception is when you assign an int to a float variable. As an int is a float rounded, it is possible to make the conversion from int to float without losses, so there is no mistake in this case.

Return types

As well as in type declarations, it is now possible to define return types of functions and methods.

The above code will run normally in the coercive mode. From the moment you add the strict_types=1, PHP will return the following error:

Fatal error: Uncaught TypeError: Return value of () must be of the type boolean, integer returned

Anonymous Classes

This new feature lets you declare simple classes, to be used only once, as follows:

Fatal Errors Converted into Exceptions

One of the best new features in my opinion is the conversion of several fatal errors in exceptions. This means that errors that would disrupt the execution in PHP 5+ can now be treated with a try/catch block. This gives a better error handling within classes this decreasing those PHP error screens. This improvement comes  with the creation of /EngineException, which can be used to handle the errors.

Use Classes in Namespaces

The use of several classes in the same namespace is largely simplified. Now you can do the following:

This will save you a few lines of code 😉

Removed Items

Some things have been removed and will no longer work in PHP 7. These items were already considered deprecated, so no one should be using. But there’s definitely a lot of code using this around, so a lot of people are going to have headaches when upgrading.

Tags

ASP-style tags (<%<%= and %>), were removed as well as script type tags (<script language=”php”>). The recommended tag remains <?php. Some functions were also removed, for example split.

Functions

Among these functions are all ereg functions (ereg_*). Now should use preg_*. Another extension that was abandoned was mysql and its mysql_* functions. But don’t worry, you can still access the MySQL database using the mysqli extension and the mysqli_* functions. In fact, you should already be using them – if you’re not, you can start to replace.

PHP 4 Constructors

In PHP 4 (and 5) it was possible to declare constructors with the same name as the class:

From now on only _construct() will be allowed. It will still be possible to use them, but you’ll get an E_DEPRECATED notice.

Conclusion

These are some of the improvements of PHP 7 most talked about. We hope that with the refactoring that has been made more improvements will arrive with the next smaller versions making PHP even better. Have your started using PHP 7 already? What improvements did you like more?

References

Leave a Reply

Your email address will not be published. Required fields are marked *