PHP 8.0 Just In Time with Attributes and much more

Published: 18.02.2018

Wait a little while and then the new version of PHP comes from version 8.0. This brings a lot of fun new functions such as the Jit Compiler, Union types, and attributes. I will briefly discuss these 3 things so that you get an idea of what the coming version entails.

Time has almost arrived and I am not talking about the birthday of Sinterklaas, no I am talking about the release of the new version of PHP. PHP 8.0 will be released on November 26. This a major release and ensures that there will be a number of important changes, as well as many new functionalities and performance improvements.

Because there are a number of important changes, you may have to update your code to make it run at PHP 8.0. But if you have kept your PHP versions up to date, the upgrade to version 8 should not be too difficult since most Breaking Changes are already outdated for the 7.* versions. And no worries, we list the outdated functions.

But before we get to the outdated functions, we will first talk about all the fun new things that PHP 8.0 entails. Such as the Union types, Jit Compiler, attributes and the str_Contains (). There are more, but those are the 4 things that we will discuss briefly.

New features

Union Types

A Union type can accept values of several different types instead of 1. In the current version of PHP this is not accepted with the exception of 2 special Union types

  • Type or null, using the special syntax ?Type.
  • Array of Traversable, using the special iterable type.
For PHP 8.0 this could be resolved with the following example by using PHPDoc Annotations. See the following example of Nikita Popov in the RFC below
Supporting Union types in the language enables us to move more type information from PHPDoc to functional signs, with the usual benefits that this entails:
  • Types are actually enforced, so mistakes can be noticed early.
  • Because they are being enforced, the chance is smaller that type information becomes outdated or missing edge cases.
  • Types are checked during inheritance, which maintains the Liskov replacement principle.
  • Types are available via Reflection.
  • The syntax is much less standard than PHPDoc.

The RFC requests whether we can support Union Types in function signs from now on, so that we are no longer dependent on inline documentation, but they can specify with the Syntax T1 | T2 ...

More about Union types

JIT Compiler

Just-in-time (JIT) Compiler is one of the features that will be set live with PHP 8.0. The Jit Compiler was already in for test purposes, but it is now ready to be used. It should ensure a significant performance increase. PHP is not compiled, but rule interpreted by rule. JIT is a technique that compiles parts of the code during Runtime, so that the compiled version can be used instead. Think of it as a "crepse version" of the interpreted code, which is generated during runtime.

Pedro Escudero has already tested JIT. It has used a simple script to test different versions of PHP Performance. He has run this script 100x and caught the average time. Below you can see the benchmark results:

Version Time in seconds
5.3 0.64574003219604
7.4 0.10253500938416
8.0 (without JIT)        0.098223924636841
8.0 (with JIT) 0.053637981414795

As you can see, the difference between 7.4 and 8.0 without jit is not very significant. But with the Jit Compiler, performance is improved by around 45%.

Attributes

Attributes are declarative metadata that between "< <" and "> >" to stand. They can be added to other parts to specify properties. Another name for attributes is Annotations. This is a term known from doctrine both are often used interchangeably. PHP has gone specifically for the name attributes to prevent the confusion with doctrine as much as possible.

More about ATTRIBUTES

str_contains()
str_Contains is one of the new functions that has taken a while. This allows you to see if a string is in a different string and you no longer have to use str_pos ().

RFC VAN STR_CONTAINS()

We have now discussed some of the large features, but there are of course more. Do you want to know what else can be done in PHP 8.0. Then click below

ALL FEATURES PHP 8.0

As you can read, version 8.0 brings us a lot of exciting and fun new features that can bring a lot of new fun things. As mentioned it is a major release so there may be things that could break your code. But if you have kept your code up-to-date, it must all be better than expected. In any case, I can't wait to play with PHP 8.0

And what can we do for you?_

Do you want to know more or are you interested? Please feel free to contact us directly, by phone or via mail.
Or use one of our contact forms so that we can answer you as quickly and appropriately as possible can give. We would love to hear from you!

back to top