PHP7 is here. Well, the final release hasn’t been released yet, but next 12th November is scheduled to be released so maybe when you are reading this post the stable final release might be released. And, what does this new language version provide? The killer feature promised is performance. Zend has published the next infographic where you can see how they announce a 2x performance improvement. This might be a half of servers, software twice faster, so it is a considerable enhancement for the whole PHP ecosystem.
Zend did also publish this infographic showing the performance boost in PHP frameworks widely used, such as Magento or Drupal. This infographic shows how PHP7 is capable of serving more requests per second than even HHVM, Facebook’s project to improve PHP performance. At the bottom of the infographic, it is shown how PHP is the fastest dynamic language resolving Mandelbrot fractals problem. So, we have again another reason to use PHP, performance.
PHP7 also provides new features, that have been added to the language to make it more modern. Syntactic sugar is always a good thing, that is why we are going to enumerate some of these new features in the language.
In the first place, we are going to explain the weird new feature, the Spaceship operator (<=>). This operator compares values returning what is expected by comparators, -1 if the first operand is less than the second, 0 if both operands are equal and 1 if the first operador is greater than the second.
This operator is not a huge boost, but it is another weird thing more to the language. The only use case that comes to my mind is to use it inside anonymous functions used by sorting functions, like usort.
A language with better typing
PHP is a dynamic language, but sometimes it is useful to be able to define the type of a function argument or which type a function returns. In previous PHP versions, only class types were allowed to be defined in function arguments. PHP7 will allow also scalar types to be hinted in function arguments. It also adds the possibility to define which type a function returns. Be ready to have a better autocompletion support for the language in IDEs, because I suppose almost all PHP7 frameworks will make an extensive use of this new feature.
This flow structure has been the most written piece of code during my professional PHP life.
Don’t you agree? Haven’t you ever used this flow structure? To avoid a Notice when you don’t check if the key exists in an array, you are forced to write this piece of code. Some developers used the @ operator, that suppresses warnings generated by the statement surrounded by it. That is not a solution, that was just hiding an error.
In PHP7, we will be able to use the coalesce operator that will allow a neater way to solve this problem.
Group use declarations
From PHP5.3 forward, PHP has namespaces. If you use classes from another namespace, the use statement allows to use simply the class name instead of its full namespace name. Before PHP7, you had to write a use statement for each class. With PHP7, group use declarations can be used, writing only one line per package making it clearer what classes and namespaces are being used.
intdiv function will provide a way to execute the integer division without any trick.
Expressions in constants
Now, constants will be able to store the result of an expression. Before, PHP threw an exception.
The biggest of problems with new major language versions, it’s cool to introduce new features and huge improvements but normally these features introduce BC breaks (backwards compatibility breaks). So, if a project is not under automatic testing or, at least, has a manual procedure established to know it does what is expected, we have a great problem if we have to migrate it to a newer language major version.
Regarding PHP7, it has been promised it won’t be a pain to migrate from PHP5 to it. Even though, some changes are worth to take a look before moving on.
Exception handling changes
The exception hierarchy has been modified. In PHP7 we will have exceptions and errors. Some structures that threw an uncatchable fatal error, now will throw an Error that could be catched with a try/catch statement, because this Error class will implement the Throwable interface that will be the new way to catch any exception or error in a block of code.
This will allow catching errors that before stopped the program flow, or simply triggered unmeaningful warnings, like the division by zero. Now, we can even catch a division by zero error and do something special about it.
New Objects cannot be assigned by reference
In PHP7, an object created with a new statement cannot be assigned by reference in the same statement to a variable. This piece of code was widely used in previous PHP versions because of some performance reasons, so if you are migrating a legacy project or you’re used to use this piece of code in your applications, it’s a good moment to stop using it.
More BC issues
PHP7 is here to make the Web better
In my opinion, it is a new version with little breaking compatibility changes, something positive, but it this mantra has also made impossible to do more ambitious changes to the language that might have considerably improved the language. But I suppose PHP mantainers don’t want a Python3 happening in PHP.
Other features I miss are more types and functions for the PHP internals. A decimal type would be great to do fixed numeric operations, something basic for high-level languages. So, to finish with this post, developers must keep updated, so it is important to know which are the new features provided by this new version because sooner or later this version will become the standard. And the sooner the better. PHP7 has performance and behaviour improvements that can’t be skipped. PHP7 is here to make the Web better.