background

PHP 8.0, what's new?

PHP 8.0 brings a host of enhancements aimed at making the language more organized and intuitive. In this article, we’ll highlight key new features like the match expression, constructor property promotion, and the Nullsafe operator.

Blog 2023-01-22

Overview

After major updates, there's always a lot to look forward to — new methods, performance boosts, and exciting new features. PHP 8.0 brings significant improvements in performance, syntax simplification, and developer experience. Let’s break down the most important changes.

1. Performance Enhancements

Just-In-Time (JIT) Compilation

PHP 8.0 introduced JIT compilation, which compiles PHP bytecode into machine code at runtime, offering significant performance improvements in computational-heavy tasks.

How JIT Works:

Unlike traditional PHP execution (interpreted via Zend Engine), JIT translates PHP code into machine code just before execution, reducing overhead.

JIT in Action

function fibonacci(int $n): int {
    if ($n <= 1) return $n;
    return fibonacci($n - 1) + fibonacci($n - 2);
}

$start = microtime(true);
echo fibonacci(35) . PHP_EOL;
echo "Execution time: " . (microtime(true) - $start) . " seconds";

With JIT enabled, execution time is reduced for computational tasks. However, standard web applications may see minimal performance improvements.


2. New Syntax Features

Constructor Property Promotion

Before PHP 8.0, defining class properties required explicit assignments in the constructor. With constructor property promotion, boilerplate code is significantly reduced.

Before:

class Person {
    private string $name;
    private int $age;

    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
    }
}

After PHP 8.0:

class Person {
    public function __construct(private string $name, private int $age) {}
}

This improves readability and maintainability.


Named Arguments

PHP 8.0 introduces named arguments, allowing developers to specify function parameters explicitly by name instead of relying on argument order.

Example:

function createPost(string $title, string $content, ?string $author = null, ?array $tags = null) {}

createPost(title: "New Post", content: "PHP 8 is awesome!", tags: ["PHP", "Update"]);

This provides better readability and avoids passing unnecessary null values.


Nullsafe Operator

Before PHP 8.0, checking for null values required verbose conditional checks:

$products = null;
$cart = $user ? $user->getCart() : null;
$products = $cart ? $cart->getProducts() : null;

With PHP 8.0's Nullsafe Operator (?->), this can be simplified:

$products = $user?->getCart()?->getProducts();

This prevents errors when chaining method calls on potentially null values.


Match Expression

PHP 8.0 introduces match, a more concise and powerful alternative to switch.

Example:

$lang = $this->getLang();

$message = match ($lang) {
    'pl_PL' => 'Witam!',
    'en_US' => 'Hello!',
    default => 'Hello!'
};

Key benefits:

  • No need for break statements.
  • Supports returning values directly.
  • Uses strict comparisons.

3. New Functions

PHP 8.0 brings several new useful functions:

str_contains()

A simple way to check if a substring exists within a string.

$found = str_contains('Keep it simple stupid', 'simple'); // true

str_starts_with() and str_ends_with()

Useful for checking prefixes and suffixes in strings.

str_starts_with('PHP 8 is fast', 'PHP'); // true
str_ends_with('PHP 8 is fast', 'fast'); // true

get_debug_type()

An improved version of gettype(), returning more precise type information.

$var = new DateTime();
echo get_debug_type($var); // "DateTime"

4. Deprecations & Breaking Changes

Removed Functions:

  • create_function() – Deprecated in PHP 7.2, removed in PHP 8.0.
  • Magic Quotes (get_magic_quotes_gpc()) – Finally removed.

Deprecated Features:

  • Implicit conversions of null to string (null . 'string') now throw a warning.
  • each() function is removed (use foreach instead).

Summary

PHP 8.0 introduces massive improvements in performance, readability, and developer experience. With new syntax features like match expressions, named arguments, and constructor property promotion, coding in PHP has never been more intuitive.

🚀 Upgrading to PHP 8.0 is highly recommended for anyone looking to build modern, high-performance applications.