Opcode Optimisation
debt(d5/e1/b3/t5)
Closest to 'specialist tool catches it' (d5). The detection_hints list opcache-gui, vld, and blackfire — all specialist tools requiring deliberate setup. A developer won't know OPcache is misconfigured or that optimization_level is suboptimal without querying phpinfo(), using opcache-gui, or profiling with blackfire. No default linter flags this; it requires intentional inspection.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: set opcache.optimization_level=0x7FFFBFFF in php.ini — a single configuration line. Removing manually pre-computed constants is equally a local one-line change per occurrence.
Closest to 'localised tax' (b3). The concern is scoped to PHP runtime configuration and PHP files that misuse manual micro-optimisations. Once OPcache is correctly configured, no ongoing burden exists. It applies to web and cli contexts but doesn't reshape architecture or slow down multiple workstreams once addressed.
Closest to 'notable trap' (t5). The misconception field explicitly states that developers believe manual pre-computation of constants (e.g. 60*60*24) improves performance, when OPcache's constant folding already handles this at compile time. This is a documented, well-known PHP gotcha that most developers learn after profiling — it contradicts intuition but is not catastrophic, placing it firmly at t5.
Also Known As
TL;DR
Explanation
PHP's Optimizer (part of OPcache) applies multiple optimisation passes to compiled opcodes: constant folding (2 + 3 compiled to 5), dead code elimination (if (false) { } removed), array/string merging (concatenated literals merged), pass-through optimisation (unnecessary RETURN opcodes removed), and live variable analysis (unused variables eliminated). PHP 7+ uses type inference during optimisation, enabling more aggressive dead code elimination with typed code. These optimisations happen once at compile time and are cached — they have zero runtime overhead once in OPcache.
Common Misconception
Why It Matters
Common Mistakes
- Pre-computing constants manually — PHP optimiser does this at compile time.
- Avoiding clear code for micro-optimisation — the optimiser handles constant expressions.
- Not enabling OPcache — without it, no optimisations apply on any request.
- Thinking opcache.optimization_level controls all passes — different passes have different levels.
Code Examples
// Unnecessary manual micro-optimisation:
$secsPerDay = 86400; // Pre-computed manually
$secsPerWeek = 604800; // Clutter — compiler does this
$bytesPerMB = 1048576;
// Complicated null check to avoid function call:
if (isset($arr['key']) && $arr['key'] !== null) { } // Premature
// Let the compiler optimise:
$secsPerDay = 60 * 60 * 24; // Compiler folds to 86400
$secsPerWeek = 60 * 60 * 24 * 7; // Compiler folds to 604800
$bytesPerMB = 1024 * 1024; // Compiler folds to 1048576
// Readable null check:
if (isset($arr['key'])) { } // Clear intent, same performance
// View opcache stats:
$status = opcache_get_status();
// Check: ['opcache_statistics']['hits'] vs ['misses']
// High miss rate = too many files or opcache.max_accelerated_files too low