Modern PHP Is Not What You Remember

PHP 8.x has enums, fibers, readonly properties, and a proper type system. It's worth a second look.

Key Insights

  • PHP 8.1+ has native enums, fibers, readonly properties, and intersection types
  • Named arguments and match expressions make code significantly more readable
  • Modern PHP with strict types is a genuinely productive language

Enums (PHP 8.1)

enum Status: string {
    case Active = 'active';
    case Inactive = 'inactive';
    case Pending = 'pending';

    public function label(): string {
        return match($this) {
            self::Active => 'Active',
            self::Inactive => 'Inactive',
            self::Pending => 'Pending Review',
        };
    }
}

Readonly Properties (PHP 8.2)

readonly class Point {
    public function __construct(
        public float $x,
        public float $y,
    ) {}
}

Fibers for Async

$fiber = new Fiber(function (): void {
    $value = Fiber::suspend('paused');
    echo "Resumed with: $value";
});

$result = $fiber->start(); // 'paused'
$fiber->resume('hello');   // prints "Resumed with: hello"

Liked this? There's more.

Every week: one practical technique, explained simply, with code you can use immediately.