Lab 02: Readonly & Enums
Step 1: Readonly Properties (PHP 8.1)
<?php
class Point {
public function __construct(
public readonly float $x,
public readonly float $y,
) {}
}
$p = new Point(3.0, 4.0);
echo "$p->x, $p->y\n"; // 3, 4
try {
$p->x = 1.0; // Fatal error
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}Step 2: Readonly Classes (PHP 8.2)
Step 3: Pure Enums
Step 4: Backed Enums (int and string)
Step 5: Enum with Interface and Constants
Step 6: Enum in match Expression
Step 7: Enum as Type — No Invalid States
Step 8: Capstone — Immutable State Machine
Summary
Feature
Syntax
PHP Version
Last updated
