100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

PHP 8 New Features Cheat Sheet

PHP 8 New Features Cheat Sheet

Covers PHP 8.0-8.4 additions: named arguments, enums, readonly properties, match expressions, attributes, fibers, and typed class constants.

2 PagesIntermediateJan 8, 2026

Named Arguments & `match`

Named args (8.0) improve call-site readability; `match` (8.0) is a strict, expression-based switch.

php
function createUser(string $name, string $role = 'user', bool $active = true) {}createUser(name: 'Ada', role: 'admin'); // skip positional order, skip $active$status = 200;$message = match (true) {    $status >= 200 && $status < 300 => 'Success',    $status >= 400 && $status < 500 => 'Client Error',    $status >= 500 => 'Server Error',    default => 'Unknown',};// match uses strict (===) comparison and has no fallthrough

Enums & Readonly Properties

Backed enums (8.1) and readonly properties (8.1) for immutable, type-safe value objects.

php
enum Status: string{    case Draft = 'draft';    case Published = 'published';    case Archived = 'archived';    public function label(): string    {        return match ($this) {            self::Draft => 'Draft',            self::Published => 'Published',            self::Archived => 'Archived',        };    }}class Point{    public function __construct(        public readonly float $x,        public readonly float $y,    ) {}}$p = new Point(1.0, 2.0);// $p->x = 5.0; // Error: Cannot modify readonly property Point::$x

Attributes & Nullsafe Operator

Attributes (8.0) replace docblock annotations; `?->` (8.0) short-circuits on null.

php
#[Attribute]class Route{    public function __construct(public string $path, public string $method = 'GET') {}}class UserController{    #[Route('/users/{id}', method: 'GET')]    public function show(int $id) {}}// Nullsafe chaining — returns null instead of throwing if any link is null$city = $user?->getAddress()?->getCity();// Constructor property promotion (8.0)class Money{    public function __construct(        private int $amount,        private string $currency = 'USD',    ) {}}

PHP 8.2-8.4 Additions

Readonly classes, typed class constants, and the new array_find family.

php
// 8.2: readonly classes — every property is readonly automaticallyreadonly class Coordinates{    public function __construct(        public float $lat,        public float $lng,    ) {}}// 8.3: typed class constantsclass Config{    public const string VERSION = '2.4.0';}// 8.4: new array functions$users = [['name' => 'Ada', 'age' => 30], ['name' => 'Grace', 'age' => 25]];$found = array_find($users, fn($u) => $u['age'] < 28);// property hooks (8.4) — computed getters/setters without boilerplateclass Temperature{    public float $celsius = 0;    public float $fahrenheit {        get => $this->celsius * 9 / 5 + 32;        set => $this->celsius = ($value - 32) * 5 / 9;    }}

Feature-to-Version Map

Quick lookup for which release introduced what.

  • 8.0- named args, match, nullsafe ?->, attributes, union types, constructor promotion
  • 8.1- enums, readonly properties, fibers, never return type, first-class callable syntax
  • 8.2- readonly classes, disjunctive normal form (DNF) types, standalone null/false types
  • 8.3- typed class constants, #[Override] attribute, json_validate()
  • 8.4- property hooks, asymmetric visibility, array_find/array_any/array_all
  • Deprecated in 8.x- dynamic properties (8.2), implicit nullable types warning tightened
Pro Tip

Use `#[\Override]` (8.3) on methods that override a parent method — it's a zero-cost compile-time check that catches typos and broken overrides when a parent class signature changes.

Was this cheat sheet helpful?

Explore Topics

#PHP8NewFeatures#PHP8NewFeaturesCheatSheet#Programming#Intermediate#NamedArgumentsMatch#EnumsReadonlyProperties#AttributesNullsafeOperator#PHP8284Additions#OOP#CheatSheet#SkillVeris