devnotes.

Modern PHP 8.x Features

PHP has evolved enormously since PHP 7. Here are the features most worth knowing, current through PHP 8.5 (Nov 2025):

  • Readonly properties (8.1+) — a property that can be set once, in the constructor, and never changed again: public readonly string $id;. Great for immutable value objects.
  • Enums (8.1+) — covered in the OOP Advanced chapter; type-safe fixed sets of values.
  • First-class callable syntax (8.1+) — $fn = strlen(...); instead of 'strlen' or Closure::fromCallable().
  • Named arguments (8.0+) and constructor property promotion (8.0+) — covered earlier; reduce boilerplate significantly.
  • Nullsafe operator (8.0+) — $user?->address?->city returns null instead of throwing if any link in the chain is null, replacing verbose nested isset() checks.
  • Union and intersection types (8.0+ / 8.1+) — function process(int|string $id) and function log(Countable&Iterator $items).
  • Readonly classes (8.2+) — mark an entire class readonly at once instead of every property individually.
  • New in initializers (8.1+) — default parameter values can be new object instances: function __construct(private Logger $logger = new NullLogger()) {}.
  • Fibers (8.1+) — lightweight primitives for cooperative multitasking, the low-level foundation modern async PHP libraries build on.
  • json_validate() (8.3+) — check if a string is valid JSON without the overhead of fully decoding it.
  • Property hooks (8.4+) — define custom get/set logic directly on a property declaration, reducing the need for manual getter/setter boilerplate.

General best practices across any modern PHP codebase: always declare parameter and return types; enable declare(strict_types=1);; follow PSR-12 coding style; use Composer and PSR-4 autoloading; write tests (PHPUnit or Pest); use static analysis tools like PHPStan or Psalm to catch bugs before runtime; and prefer immutability (readonly properties, avoiding unnecessary global state) wherever practical.

Modern PHP 8.x Features and Best Practices | DevNotes