← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Named Arguments with Spread Operator

PHP PHP 8.1+ Intermediate
debt(d5/e3/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list Rector and PHPStan as tools — both are specialist static analysis tools rather than default linters. PHPStan can flag incorrect spread usage (wrong key types, version incompatibility) and Rector can identify old call_user_func_array patterns that could be modernised. These tools require deliberate integration and configuration, placing this squarely at d5.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is straightforward — replace call_user_func_array() or manually built argument arrays with fn(...$namedArgs) — but it requires verifying that array keys match parameter names and checking the PHP version floor is 8.1+. This is a small, targeted refactor within one component rather than a single-line swap, placing it at e3.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to scope covers web, cli, and queue-worker contexts broadly, but the actual burden is localised to call sites using dynamic dispatch patterns. Most of the codebase is unaffected; only functions that receive or forward argument arrays pay the tax of ensuring string keys match parameter names. This is a recurring but contained concern, placing it at b3.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field explicitly states that developers believe named arguments and spread cannot be combined at all — a documented PHP 8.1 gotcha. The common_mistakes reinforce this: numerically indexed arrays silently fail or error, pre-8.1 code breaks, and mixing positional spread with named arguments creates ambiguity. These are well-documented but non-obvious surprises that most developers learn only after encountering them, placing this at t5.

About DEBT scoring →

Also Known As

named args spread spread named arguments PHP 8.1 spread

TL;DR

Combining PHP 8.0 named arguments with the ... spread operator to unpack associative arrays as named parameters.

Explanation

PHP 8.1 extended the spread operator to support string-keyed arrays, enabling named argument unpacking: function create(string $name, int $age): User {} can be called with $args = ['name' => 'Alice', 'age' => 30]; create(...$args). This is particularly useful for forwarding configuration arrays to functions, building flexible factory methods, and passing captured named arguments between layers. The spread array must use string keys matching parameter names exactly — positional and named spreads cannot be mixed. Combined with named arguments (PHP 8.0), this enables highly readable and order-independent API surfaces.

Common Misconception

Named arguments and the spread operator cannot be combined. PHP 8.1 allows spreading named arguments from an associative array into a function call — ...$args where $args has string keys passes each as a named argument, enabling dynamic named argument dispatch.

Why It Matters

Combining named arguments with the spread operator allows passing pre-built argument arrays to functions by name — enabling dynamic dispatch while keeping code readable.

Common Mistakes

  • Spreading a numerically indexed array into a function expecting named arguments — keys must match parameter names.
  • Using spread with named arguments before PHP 8.1 where string key spread was not yet supported.
  • Not realising that named argument spread does not support reordering — array keys must match, not positions.
  • Mixing positional spread and named arguments in a way that creates ambiguity — keep them separate.

Code Examples

✗ Vulnerable
// Positional spread — fragile, order-dependent:
function createUser(string $name, string $email, int $role): User {}
$args = ['Alice', 'alice@example.com', 1];
createUser(...$args); // Breaks if param order changes

// Named spread — order-independent:
$args = ['email' => 'alice@example.com', 'name' => 'Alice', 'role' => 1];
createUser(...$args);
✓ Fixed
$args = ['name' => 'Alice', 'role' => 'admin'];
$user = createUser(...$args); // spreads as named args

Added 15 Mar 2026
Edited 22 Mar 2026
Views 87
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings M 1 ping T 1 ping W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 1 ping T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 PetalBot 8 Ahrefs 7 ChatGPT 6 SEMrush 6 Scrapy 5 Perplexity 4 Unknown AI 3 Google 3 Brave Search 3 Applebot 2 Majestic 1 Meta AI 1 Twitter/X 1 Bing 1
crawler 54 crawler_json 4 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use named argument spread to pass associative arrays as named arguments: fn(...$namedArgs) — this lets you build argument arrays programmatically and pass them cleanly
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
call_user_func_array() still used where named spread would work; building argument arrays for call_user_func that PHP 8.1 spread handles natively
Auto-detectable: ✓ Yes rector phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant