Backend Development 12 min read

New Features and Performance Improvements in PHP 8.0

This article outlines the major new features and performance enhancements introduced in PHP 8.0, including JIT compilation, named parameters, union types, attributes, match expressions, null‑safe operator, WeakMap, constructor property promotion, and numerous syntax improvements, accompanied by illustrative code examples.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
New Features and Performance Improvements in PHP 8.0

PHP 8.0, officially released on 26 November 2020, brings a substantial set of language enhancements and performance optimizations.

Key improvements include a Just‑In‑Time (JIT) compiler that can boost script execution speed, core JSON support, named parameters, union types, static return types, and a new mixed type that represents all possible value types.

Several new functions simplify string handling, such as str_contains() , str_starts_with() and str_ends_with() . The null‑safe operator ?-> prevents errors when chaining method calls on null values.

New syntax features include optional trailing commas in parameter lists, variadic parameters in overridden methods, and constructor property promotion that merges property declaration with constructor arguments.

Example of a new ValueError exception:

<?php
declare(strict_types=1);
/**
 * Passing an empty array to array_rand triggers a ValueError.
 */
array_rand([], 0);
/**
 * Providing an invalid depth to json_decode also throws ValueError.
 */
json_decode('{}', true, -1);
?>

Union types allow a variable to hold values of multiple types:

<?php
declare(strict_types=1);
class Number {
    private int|float $number;
    public function setNumber(int|float $number): void { $this->number = $number; }
    public function getNumber(): int|float { return $this->number; }
}
$number = new Number();
$number->setNumber(5);
var_dump($number->getNumber());
$number->setNumber(11.54);
var_dump($number->getNumber());
?>

Variadic parameters can replace any number of arguments when overriding methods:

<?php
declare(strict_types=1);
class A { public function method(int $many, string $parameters, $here) {} }
class B extends A { public function method(...$everything) { var_dump($everything); } }
$b = new B();
$b->method('i can be overwritten!');
?>

Static return types enable fluent interfaces while preserving the concrete class type:

<?php
declare(strict_types=1);
class Test { public function doWhatever(): static { return $this; } }
?>

The new WeakMap class provides a map from objects to values without preventing garbage collection of the keys:

<?php
declare(strict_types=1);
class FooBar {
    public WeakMap $cache;
    public function __construct() { $this->cache = new WeakMap(); }
    public function getSomethingWithCaching(object $obj) {
        return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj);
    }
    private function computeSomethingExpensive(object $obj) { var_dump("I got called"); return rand(1,100); }
}
$cacheObject = new stdClass;
$obj = new FooBar;
$obj->getSomethingWithCaching($cacheObject);
$obj->getSomethingWithCaching($cacheObject);
var_dump(count($obj->cache));
unset($cacheObject);
var_dump(count($obj->cache));
?>

Attributes (annotations) are now first‑class language constructs, enabling metadata on classes, methods, and parameters:

<?php
declare(strict_types=1);
#[Attribute]
class ApplyMiddleware {
    public array $middleware = [];
    public function __construct(...$middleware) { $this->middleware = $middleware; }
}
#[ApplyMiddleware('auth')]
class MyController { public function index() {} }
$reflection = new ReflectionClass(MyController::class);
$attributes = $reflection->getAttributes(ApplyMiddleware::class);
foreach ($attributes as $attr) { var_dump($attr->newInstance()->middleware); }
?>

Constructor property promotion reduces boilerplate when defining simple data objects:

<?php
declare(strict_types=1);
class User {
    public function __construct(public int $id, public string $name) {}
}
$user = new User(1, 'Marcel');
var_dump($user->id);
var_dump($user->name);
?>

Traits can now declare abstract private methods, allowing more flexible composition:

<?php
declare(strict_types=1);
trait MyTrait {
    abstract private function neededByTheTrait(): string;
    public function doSomething() { return strlen($this->neededByTheTrait()); }
}
class TraitUser { use MyTrait; private function neededByTheTrait(): string { return 'data'; } }
?>

The match expression provides a safer, value‑returning alternative to switch :

<?php
declare(strict_types=1);
echo match (1) {
    0 => 'Foo',
    1 => 'Bar',
    2 => 'Baz',
};
?>

Throw can now be used as an expression, enabling concise error handling in null‑coalescing and ternary contexts:

<?php
declare(strict_types=1);
$callable = fn() => throw new Exception();
$nullable = null;
$value = $nullable ?? throw new InvalidArgumentException();
?>

Catching an exception without binding it to a variable is also supported:

<?php
declare(strict_types=1);
try {
    $value = $nullable ?? throw new InvalidArgumentException();
} catch (InvalidArgumentException) { var_dump('Something went wrong'); }
?>

Other notable additions include the Stringable interface, support for the ?-> null‑safe operator, and the ability to use class name literals via $object::class .

Overall, PHP 8.0 delivers a modernized syntax, richer type system, and performance gains that make it a compelling upgrade for backend development.

performanceBackend Developmentcode examplesNew FeaturesPHP 8
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.