PHP 8: What you need to know about the latest update

PHP is one of the programming languages that is easiest to learn. With a PHP tutorial and the right motivation, you can write the first scripts and execute commands within a few hours. The current version of PHP (PHP 7) introduced speed optimization, which led to an improved ranking. Version 8 of PHP further improves on this and introduces other new features.

Developer Brent Roose recently made an announcement about all the new features of the PHP 8 release. As work on the new version continues, more changes are likely to be announced in the coming months until the final release.

When will PHP 8 be released?

After a lot of to-ing and fro-ing as to when PHP 8 would be released, the developers have now announced that November 26th, 2020 is the date we’re waiting for. This corresponds to the normal three-year cycle for releasing an updated PHP. PHP 7.4 was released in December 2019 and the support for PHP 7.1 has been discontinued. Many websites written with PHP are still using an old version that soon will no longer be supported. Keeping the code of your website up to date and using new versions of PHP has its advantages. New features offer greater variety, improvements in performance, and security issues can be fixed.

Try out PHP 8 now!

As a IONOS web hosting customer you now have the option to test the Beta version of PHP 8 ahead of its release. Not yet a customer? We have several low-cost web hosting packages to choose from. Once signed up, you can change the PHP version you’re using in your IONOS customer account and upgrade to PHP 8 Beta. Please keep in mind that the use of PHP 8 Beta is not recommended for production systems.

Fact

The first alpha versions of PHP 8 have been released. However, these are just for testing purposes and should not be used in online versions of websites or apps, for example.

What are the problems with the old code?

Because PHP 8 is a major new release, one should expect old code to no longer be compatible. However, most of the changes that could lead to complications were already addressed in versions 7.2, 7.3, and 7.4.

The latest changes include:

  • The real type
  • Magic quotes legacy
  • array_key_exists() with objects
  • FILTER_SANITIZE_MAGIC_QUOTES filter
  • Reflection export() methods
  • mb_strrpos() with encoding as 3rd argument
  • implode() parameter order mix
  • Unbinding $this from non-static closures
  • hebrevc() function
  • convert_cyr_string() function
  • money_format() function
  • ezmlm_hash() function
  • restore_include_path() function
  • allow_url_include ini directive

If you have always kept your code up to date, you won’t have any problems migrating to PHP 8, even if the new update will be a major release. A complete list of changes can be found in the upgrade notes on GitHub

Note

Microsoft announced that they will no longer offer Windows support for PHP 8 and later versions. However, the PHP developers have already said that they want to relate to this themselves, so Microsoft fans have nothing to fear.

These are the new PHP 8 features

The new version of PHP is expected to come with some new features that offer web developers many additional possibilities.

JIT Compiler

Among the major new feature of PHP 8 is the JIT compiler, which should improve performance significantly. PHP is not compiled, but interpreted line by line. The JIT compiler (Just in Time) works by compiling parts of the code during runtime - and in doing so acts very much like a cached version of the code. This should lead to a massive improvement in performance.

This new feature in PHP 8 has already been tested by Pedro Escudero. He used a simple script to compare versions 5.3, 7.4, and 8 (with and without JIT). For this purpose, he ran the script 100 times in each version and then calculated the average time.

The following average values were the results of his testing:

Version Time in Seconds
5.3 0.64574003219604
7.4 0.10253500938416
8 (without JIT) 0.098223924636841
8 (with JIT) 0.053637981414795

Even if changing from version 7.4 to 8 without JIT does not make much difference, the difference to version 8 with JIT is significant. The JIT Compiler improves performance by more than 45%.

JSON extension always activated

In earlier versions of the scripting language, the JSON extension had the option to be turned off. Since JSON is such an important data format, however, the PHP developers decided to ensure that the extension activated at all times. It is assumed that this simplifies working with PHP.

Union Types

Union Types are known from other languages like C/C++, TypeScript or Haskell. Here two or more types can form a union and any of the types mentioned can be used. In the code itself this can look like this:

public function foo(Foo|Bar $input): int|float;

However, there is one restriction, because void cannot become part of a union type; it does not output a return value. Also, nullable unions can be declared with |null or ? as in this example:

public function foo(Foo|null $foo): void;
public function bar(?Bar $bar): void;

Static return type

Static is a special class name and becomes a valid return type in the new version besides self & parent.

WeakMap

WeakRefs were already added in PHP 7.4. With PHP 8, however, WeakMaps have been included which are the extension of this function. WeakMaps and WeakRefs can be used to delete objects when only the cache references the entity classes of the objects. This leads to resource-saving handling of the objects. An example from the documentation is as follows:

class FooBar {
    private WeakMap $cache;
    public function getSomethingWithCaching(object $obj) {
        return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj);
    }
    // ...
}

Use ::class for objects

In earlier versions, you had to use get_class() to assign a class to objects. But now, you can apply ::class to objects. This ensures that the source code is kept slimmer.

Stringable interface

The stringable interface implements an interface automatically. Previously, this step had to be done manually. This can be done for all strings or where __toString() is implemented. In the code itself it looks like this:

class Foo
{
    public function __toString(): string
    {
        return 'foo';
    }
}
function bar(Stringable $stringable) { /* … */ }
bar(new Foo());
bar('abc');

Convert DateTime

With PHP 8 it is much easier to convert DateTime. The developers have added DateTime::createFromInterface() and DatetimeImmutable::createFromInterface(). This makes it possible to convert DateTime and DateTimeImmutable .

fdiv

Using fdiv division with 0 is allowed. You now get INF, -INF or NAN as return value.

Type annotations

The new version adds correct type annotations for all internal functions and methods.

Type errors

Previously, only user-defined functions triggered TypeErrors. Internal functions issued a warning and null. Starting with PHP 8, internal functions will also output TypeErrors.

Reclassified engine warnings

Till PHP 8, there were lots of errors that only issued a warning or note. This has now been updated. A full listing of the new PHP error messages can be found in the documentation.

Default error reporting level

Errors that were previously ignored will be output as: E_ALL, E_NOTICE, and E_DEPRECATED. These errors already existed in previous versions, but were not clearly marked.

@ won’t supress errors aymore

This new feature allows errors to be detected, too. Instead of suppressing them with the @-operator, as was previously the case, you should now make sure that you have set display_errors=Off on the server.

Order of string operators

This feature was introduced with PHP 7.4 but now is taking full effect. PHP 8 reacts more intelligently when working with multiple operators. The following code is a good example:

echo "sum: " . $a + $b;

This was previously interpreted by PHP as:

 echo ("sum: " . $a) + $b;

PHP 8 now turns it into:

echo "sum: " . ($a + $b);

Reflection

The signatures have been changed. Originally, Reflection was specified as follows:

ReflectionClass::newInstance($args);
ReflectionFunction::invoke($args);
ReflectionMethod::invoke($object, $args);

However, in PHP 8 it is now written like this:

ReflectionClass::newInstance(...$args);
ReflectionFunction::invoke(...$args);
ReflectionMethod::invoke($object, ...$args);

To support PHP 7 & PHP 8 at the same time, you should proceed as follows:

ReflectionClass::newInstance($arg = null, ...$args);
ReflectionFunction::invoke($arg = null, ...$args);
ReflectionMethod::invoke($object, $arg = null, ...$args);

These PHP 8 features will definitely roll out. However, additional new features are expected to be included and announced with the release of PHP 8.


Wait! We’ve got something for you!
Have a look at our great prices for different domain extensions.


Enter the web address of your choice in the search bar to check its availability.
.org
$1/1st year
then $20/year
.com
$1/1st year
then $15/year
.info
$1/1st year
then $20/year
.me
$1/1st year
then $20/year