Modern PHP Syntax

Write PHP 8 with types, match, enum and readonly, and know which features are safe for a WordPress.org plugin.

Code written for PHP 5.6 still runs. That is the problem. No types, switch with forgotten break, isset() chains five levels deep. PHP 8 removes whole classes of bugs, but one enum in a plugin with Requires PHP: 7.4 breaks real sites. This module shows both sides.

CoursePHP for WordPress Developers · Module 2 of 9 · Stage 1 "Language"
LearnTypes, match, nullsafe, enum, readonly and what each one requires from the host
HowShort lessons with code from real plugins, IDE practice in the browser, interview questions after every lesson

The idea in one picture

flowchart LR
  L["Legacy 7.x<br/>for comparison only"] -.-> B["PHP 8.0<br/>union · match · nullsafe"]
  B --> C["PHP 8.1<br/>enum · readonly"]
  C --> N["8.2 → 8.5<br/>what each release adds"]
  N --> D["Requires PHP<br/>in the plugin header"]
  classDef step fill:#f5f3ff,stroke:#777BB4,color:#1e1b4b
  classDef key fill:#777BB4,stroke:#4f5b93,color:#ffffff

Every feature has a minimum version. The plugin header says which one your users need.

Before → after

BeforeAfter Module 2
Untyped arguments and silent bugsTypes and declare(strict_types=1) catch them early
switch with a missing breakmatch returns a value and has no fall-through
isset($a['b']['c']) chainsNullsafe ?-> and the ?? operator
Class constants as fake enumsNative enum

You'll be able to answer

  • What does declare(strict_types=1) change and where does it apply?
  • How does match differ from switch?
  • How do you use PHP 8.1 features in a plugin that must run on older PHP?