Lab 05: PHP 8.x Modern Features

Objective

Master the most impactful PHP 8.0–8.3 features: match expressions, named arguments, nullsafe operator (?->), union and intersection types, readonly properties, enums with methods and interfaces, fibers for cooperative multitasking, and first-class callable syntax.

Background

PHP 8.0 (2020) was the biggest leap since PHP 7. Each new feature reduces boilerplate and improves correctness. match is a strict switch replacement. Named arguments enable skipping defaults without wrapping in arrays. readonly enforces immutability. Enums replace class-based constants with a type-safe, first-class construct. Fibers (8.1) bring low-level coroutine support — the foundation of async frameworks like ReactPHP and Revolt.

Time

30 minutes

Prerequisites

  • PHP Foundations Lab 14 (Type System)

Tools

  • Docker: zchencow/innozverse-php:latest (PHP 8.3)


Lab Instructions

Step 1: match, named arguments, nullsafe

📸 Verified Output:


Step 2: Readonly properties, enums with methods, intersection types

📸 Verified Output:


Summary

Feature
PHP
Why it matters

match

8.0

Strict, expression-based, exhaustive

Named args

8.0

Skip defaults by name; self-documenting

?-> nullsafe

8.0

Safe chaining without isset chains

readonly

8.1

Compile-time immutability enforcement

Backed enum

8.1

Type-safe constants with methods

Fibers

8.1

Cooperative multitasking primitives

readonly class

8.2

All properties readonly by default

Further Reading

Last updated