PHP 8 wasn't just a version bump — it introduced several features that genuinely change how you write everyday code. Here are the ones worth learning first.
Named arguments. You can now pass function arguments by name instead of strict position: createUser(name: 'Varun', role: 'admin'). This makes function calls with several optional parameters far more readable, and you can skip arguments that have sensible defaults instead of passing null placeholders.
Constructor property promotion. Instead of declaring class properties and then assigning them from the constructor, you can promote them directly in the constructor signature. A class that used to take 10 lines to define its properties and constructor can now take 3, with no loss of clarity.
Match expressions. An upgrade over switch that uses strict comparison, doesn't fall through between cases, and returns a value directly — $result = match($status) { 'active' => 'Live', 'draft' => 'Pending', default => 'Unknown' };. Cleaner and less error-prone than the equivalent switch statement.
Nullsafe operator. Chaining ?-> instead of -> short-circuits to null the moment any link in the chain is null, instead of throwing a fatal error. $user?->address?->city safely returns null if the user or address doesn't exist, replacing a pile of nested isset() checks.
Union types. Function signatures can now declare that a parameter or return value accepts more than one type — function format(int|float $amount): string. This makes type declarations honest about functions that legitimately handle more than one input type, instead of forcing you to drop typing entirely.
The JIT compiler. Mostly invisible to application code, but it can meaningfully improve performance for computation-heavy workloads. For typical web request/response cycles the gains are modest, but it matters for CPU-bound scripts.
None of these require you to rewrite existing code — they're additive. The practical advice is to start using named arguments and match expressions in new code immediately; both pay for themselves in readability within the first week.
Comments (0)
No comments yet — be the first to share your thoughts.
Leave a Comment