PHP 8.1 to 8.4 catch-up
Four releases of PHP added one big idea each, and all four are small enough to show in a print statement. The examples below run on this machine, so the lines are what PHP 8.5 printed from the 8.1, 8.2, 8.3 and 8.4 features they use.
Fibers recap
Fibers pause a task mid run and resume it later without threads or callbacks. Servers use them to juggle many slow waits inside one process while each task keeps its simple top to bottom shape.
<?php $fiber = new Fiber(function (): void { echo "inside\n"; }); $fiber->start(); echo "back\n";
php fiber.php
inside
back
The callable runs inside the fiber, and the line after start() runs when the fiber has nothing left to do, so the order on the page is the order of the run.
Readonly recap
Readonly properties lock a value after construction, which makes small value objects safe to share. Set every field once in the constructor and the shape can never drift later.
<?php class Point { public function __construct( public readonly int $x, public readonly int $y ) { } } $p = new Point(3, 4); echo $p->x . "\n";
php point.php
3
The promoted properties are set by the constructor and the read is the only operation the class allows on them. A second write to $p->x would fail with Cannot modify readonly property Point::$x.
Enums recap
Enums replace magic strings with named cases the engine checks for you. A Role of Admin or Guest beats a bare admin string that any typo can silently invent.
<?php enum Role: string { case Admin = "admin"; case Guest = "guest"; } $role = Role::Admin; echo $role->value . "\n";
php role.php
admin
The case is written with a class constant syntax and the backed value is what leaves through ->value, which is the string a database column stores.
8.3 and 8.4
Version 8.3 added typed class constants, so constant values now carry declared types that the engine verifies. Version 8.4 added property hooks, which let getters and setters live directly on the property with short blocks. Both ideas reward the same habits from PHP 8.0: declare types early and let the engine guard them.
<?php class Config { public const string NAME = "shop"; public string $mode = "dev" { get => strtoupper($this->mode); } } $config = new Config(); echo Config::NAME, PHP_EOL; echo $config->mode, PHP_EOL;
php config.php
shop
DEV
The constant carries its type in the declaration, and the hook decides what a read of $mode returns without a getter method. The stored value stays dev, which is why a second read gives the same answer.
Exercises
- Add a suspend() call inside the fiber and read the value start() returns.
- Write to a readonly property inside try and catch the Error.
- Add a third case to the enum and print both the name and the value.
- Give the config a setter hook that refuses an empty mode and describe the error.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a Fiber let a function do without threads?
Answer
Pause in the middle and resume later
The fiber keeps its own stack, so a pause and a resume behave like a function that waited.
-
When can a readonly property be written?
Answer
Once, from inside the class scope during construction
The write is allowed once in the declaring scope, and the constructor is the natural place.
-
What is a backed enum case?
Answer
A case with a scalar value it can give back
The
: stringor: inton the enum names the type, and each case carries its own value. -
What does a property hook replace?
Answer
A getter or setter method around one property
The
getblock runs on a read and thesetblock on a write, so the accessor lives on the property. -
What does this program print?
<?php $fiber = new Fiber(function (): void { echo "inside\n"; }); $fiber->start(); echo "back\n";Answer
inside / back
start()runs the callable to its end before the next statement of the script. -
What does this program print?
<?php class Point { public function __construct(public readonly int $x) { } } $p = new Point(3); try { $p->x = 5; } catch (Error $e) { echo $e->getMessage(); }Answer
Cannot modify readonly property Point::$x
The second write is refused with that message, and the catch arm prints it.
-
What does this program print?
<?php enum Role: string { case Admin = "admin"; case Guest = "guest"; } echo Role::Guest->value, " ", Role::Guest->name;Answer
guest Guest
The value is the backed string and the name is the case as written.
-
What does this program print?
<?php class Config { public const string NAME = "shop"; } echo Config::NAME;Answer
shop
A typed class constant is valid from PHP 8.3 and the value is read as usual.
-
What does this program print?
<?php class Box { public string $value = "abc" { get => strtoupper($this->value); } } $box = new Box(); echo $box->value;Answer
ABC
The
gethook runs on the read and returns the upper case form. -
Why is a readonly value object safe to pass around?
Answer
No code can change its fields after construction
Every field stays as the constructor set it, so two readers cannot disagree about the object.
The examples call strtoupper(). The programs on this page were run with PHP 8.5.13 (php -v) with the commands shown above.
Article author: Arthur Isaev