PHP 8, the new major PHP version, is expected to be released by the end of 2020. It's in very active development right now, so things are likely to change a lot in the upcoming months.

Some websites are already trying to create chaos around the subject, stating that PHP 8 might not come out. Mostly due to this post from Dmitry Stogov, Matthew Weier O’Phinney and Enrico Zimuel. But it doesn’t change much: PHP 8 development is still strong and fast-paced.

The Just In Time compiler is definitely the biggest deal for this version, bringing virtually no changes to how code looks like but potentially many unexpected behaviours to PHP platforms. So I strongly believe many previews will be delivered in 2020 way before we hit any alpha, to make sure all current features are at least stable.

New features

Starting with new features, remember that PHP 8 is still in active development, so this list will grow over time.

Union types

Given the dynamically typed nature of PHP, there are lots of cases where union types can be useful. Union types are a collection of two or more types which indicate that either one of those can be used.

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

Note that void can never be part of a union type, since it indicates "no return value at all". Furthermore, nullable unions can be written using |null, or by using the existing ? notation:

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

public function bar(?Bar $bar): void;

JIT

The JIT — just in time — compiler promises significant performance improvements, albeit not always within the context of web requests. There haven't been any accurate benchmarks done at this point, but they sure will come.

If you want to know more about what the JIT can do for PHP, you can read another post I wrote about it here.

Static return type

While it was already possible to return self, static wasn't a valid return type until PHP 8. Given PHP's dynamically typed nature, it's a feature that will be useful to many developers.

class Foo
{
    public function test(): static
    {
        return new static();
    }
}

Weak maps

Built upon the weakrefs RFC that was added in PHP 7.4, a WeakMap implementation is added in PHP 8. WeakMaps hold references to objects, which don't prevent those objects from being garbage collected.

Take the example of ORMs, they often implement caches which hold references to entity classes to improve the performance of relations between entities. These entity objects can not be garbage collected, as long as this cache has a reference to them, even if the cache is the only thing referencing them.

If this caching layer uses weak references and maps instead, PHP will garbage collect these objects when nothing else references them anymore. Especially in the case of ORMs, which can manage several hundreds, if not thousands of entities within a request; weak maps can offer a better, more resource friendly way of dealing with these objects.

Here's what weak maps look like, an example from the RFC:

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

The ::class field on objects

A small, yet useful, new feature: it's now possible to use ::class on objects, instead of having to use get_class() on them. It works the same way as get_class().

$foo = new Foo();

var_dump($foo::class);

Stringable interface

The Stringable interface can be used to type hint anything that is a string or implements __toString(). Furthermore, whenever a class implements __toString(), it automatically implements the interface behind the scenes and there's no need to manually implement it.

class Foo
{
    public function __toString(): string
    {
        return 'foo';
    }
}

function bar(Stringable $stringable) { /* … */ }

bar(new Foo());
bar('abc');