PHP 8: JIT, attributes, match and named arguments

Introduction

PHP 8 (2020) changes how code reads more than any release since 5: expressions replace statements, calls name their arguments, and metadata moves from doc-comments into the engine. The one-line tour below; full detail in the deep dives at the end.

Author: Arthur Isaev

match instead of switch

match is an expression with strict comparison and no fall-through — an unhandled value throws instead of silently doing nothing:

<?php namespace Aredel\Demo; $label = match ($status) { 200, 201 => 'ok', 404 => 'missing', default => 'error', }; echo $label;

Calls that read like config

Named arguments (start_index: 0) end boolean-flag guessing at call sites; attributes (#[Route(...)]) replace doc-comment annotations with reflection-readable syntax.

Stricter, with less writing

Union types (int|float), constructor promotion (declare and assign the property in the signature — see OOP in PHP), nullsafe operator (?->), and throw as an expression.

JIT: measure before celebrating

The tracing JIT speeds CPU-heavy code (parsers, serializers, math); typical web requests gain little. Enable OPcache first, measure, then experiment — same advice as every version.

Deep dives on this site

What is new in PHP 8.0 vs PHP 7 — every feature above with examples.

What is new in PHP 8.5 vs PHP 8.0 — the other end of the 8.x line.

Автор статьи: Андрей Олегович