PHP Has WeakMap Too - and It Fixes Per-Object Caching
Caching computed data per object with static arrays pins those objects in memory forever - a leak with a cache's haircut. PHP 8's WeakMap caches without owning.
Step-by-step programming tutorials on PHP, JavaScript, Laravel, WordPress and more.
Caching computed data per object with static arrays pins those objects in memory forever - a leak with a cache's haircut. PHP 8's WeakMap caches without owning.
When each match needs logic - lookups, formatting, escaping - preg_replace hits its ceiling. The callback variant runs code per match, and preg_quote keeps user input from breaking your patterns.
Mass-assignment bugs happen when request data flows unfiltered into updates. Two array functions - intersect_key and diff_key - are the terse, auditable filter.
"item10" before "item2", "Öl" after "z", uppercase clustering before lowercase - default sorts betray human expectations three different ways. PHP has a fix for each.
Keyed destructuring, foreach unpacking, swaps, nested extraction - the [$a, $b] syntax has quietly become one of PHP's best readability tools.
Every date in a month, every Monday for a year, billing cycles between two dates - DatePeriod iterates ranges natively, immutably and off-by-one-free.
Currency placement, month names, ordinal suffixes, "1.234,56" vs "1,234.56" - the intl extension knows every locale's rules, and most PHP developers never call it.
The 40-line value object - properties, constructor assignments, getters - collapses into a declaration. Modern PHP makes immutable data carriers nearly free to write.
Order statuses as loose strings invite typos, scattered labels and unguarded transitions. PHP 8.1 enums centralize the values, their behavior, and - with match - force you to handle every case.
?->, ??=, throw as an expression, and the spaceship - four small operators from recent PHP that collectively delete the noisiest guard code in legacy files.
PHP 8.1 turned functions into values you can pass, store and compose - ending the string-callable era of 'array_map("strtoupper", ...)' and its silent refactoring bugs.
fetch() still cannot report upload progress reliably - but XMLHttpRequest has done it for fifteen years. The full pattern: progress events, a PHP receiver, and the config limits that silently break big uploads.