Migrating PHP 8.0 to 8.1 introduces enums, fibers, readonly properties and tentative return types.
What It Does
Migrating PHP 8.0 to 8.1 introduces enums, fibers, readonly properties and tentative return types. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
A staging runtime matching the target PHP release and a regression test suite.
PHP 8.1 introduced enums, fibers and readonly properties. Features shown in newer snippets may require a later PHP version.
The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.
Example 1: Basic Behavior
Backed enums are available starting in PHP 8.1.
<?php
enum Status: string { case New = "new"; case Done = "done"; }
echo Status::Done->value;
Example 2: Another Case
Readonly properties add a controlled initialization constraint.
<?php
final class Invoice { public function __construct(public readonly int $cents) {} }
echo (new Invoice(600))->cents;
Practical Example: Putting It to Use
Fibers add cooperative suspension; they do not create preemptive threads.
<?php
$fiber = new Fiber(function (): int { Fiber::suspend("paused"); return 21; });
echo $fiber->start(); echo $fiber->resume();
Important Behavior and Edge Cases
Internal interface return types can trigger deprecations; verify third-party library compatibility.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| Migrating from 8.0 to PHP 8.1 | Migrating PHP 8.0 to 8.1 introduces enums, fibers, readonly properties and tentative return types. |
| Related approach | Upgrade dependencies and review deprecation output. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for Migrating from 8.0 to PHP 8.1 | Internal interface return types can trigger deprecations; verify third-party library compatibility. |
| Assuming a related mechanism has the same behavior | Upgrade dependencies and review deprecation output. |
Key Takeaways
- Migrating PHP 8.0 to 8.1 introduces enums, fibers, readonly properties and tentative return types.
- Internal interface return types can trigger deprecations; verify third-party library compatibility.
- Upgrade dependencies and review deprecation output.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.