Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Concepts and Reference

PHP 8 feature tour

A guided tour of the biggest PHP 8 features: union types, match, named arguments, nullsafe, enums, readonly, fibers, first-class callables and more.

PHP 8.0 was the biggest release in a decade, and every release since (8.1 to 8.5) has kept adding to the language. This page walks through the features that change how you write everyday code. Each item names the version that introduced it, so you can tell what your server supports.

You do not need to learn them all at once. Start with the ones that remove boilerplate (constructor promotion, match, the nullsafe operator), then adopt the type-system features (union types, enums, readonly) as you touch more code.

PHP
<?php
$city = $user?->address?->city ?? "unknown";        // 8.0 nullsafe
$label = match ($status) { 1 => "on", 0 => "off" }; // 8.0 match

Try it

PHP PHP 8.0 features: union types, match, nullsafe, named arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP PHP 8.1 and later: enums, readonly, first-class callables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP 8.0

  • Union types (int|string) and the mixed and static return types.
  • Named arguments: pass arguments by parameter name, skip defaults, in any order.
  • Constructor property promotion: declare and assign properties in the constructor signature.
  • match expression: strict comparison, no fall-through, throws UnhandledMatchError when nothing matches.
  • Nullsafe operator ?->: short-circuits a whole chain when a link is null.
  • Attributes (#[Route("/")]): structured metadata, readable through Reflection. See attributes.
  • str_contains(), str_starts_with(), str_ends_with(), get_debug_type(), and throw as an expression.
  • Saner behavior: string-to-number comparison follows new rules (loose comparison tables), and many warnings became TypeError or ValueError exceptions.

PHP 8.1

  • Enums, pure and backed, with methods and interfaces.
  • readonly properties: write once, then immutable.
  • First-class callable syntax: strlen(...), $obj->method(...).
  • Fibers: see Fibers.
  • never return type, pure intersection types (A&B), new in initializers, array_is_list(), and the 0o17 octal prefix.

PHP 8.2 to 8.5

  • 8.2: readonly classes, null, false and true as standalone types, DNF types such as (A&B)|null, constants in traits, and the #[SensitiveParameter] attribute. Dynamic properties were deprecated.
  • 8.3: typed class constants, #[Override], json_validate(), dynamic class-constant fetch (Foo::{$name}).
  • 8.4: property hooks and asymmetric visibility (public private(set)), new without extra parentheses (new Foo()->bar()), lazy objects, array_find(), array_any() and array_all().
  • 8.5: the pipe operator |>, the #[NoDiscard] attribute, array_first() and array_last(), and a built-in URI extension.

Check what your own install supports by printing PHP_VERSION and PHP_VERSION_ID.

Usage notes

  • Adopt features gradually. Code that runs on 8.0 runs on later 8.x with few changes, but read the deprecated features page before upgrading.
  • Tools such as Rector and PHP_CodeSniffer can rewrite older code to use promotion, match and typed properties automatically.
  • composer.json can pin the minimum version ("php": "^8.2"), which stops Composer from installing your package on older runtimes.