← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Spread Operator & Variadic Args (PHP 5.6)

php PHP 5.6+ Beginner
debt(d5/e3/b3/t3)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5), because detection_hints lists rector and phpcs with code_pattern matching func_get_args() and call_user_func_array() — these are specialist static analysis / code-style tools, not default linters or compiler errors. The pattern is detectable automatically but requires these tools to be configured.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3), because quick_fix describes replacing func_get_args() with ...$params and call_user_func_array($fn, $args) with $fn(...$args) — a small, mechanical pattern replacement within one component. It's more than a single-line patch because multiple call sites may need updating, but it doesn't span the whole codebase architecturally.

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

Closest to 'localised tax' (b3), because the applies_to scope is broad (web, cli, queue-worker) but the common_mistakes are localised to function signatures and call sites. The burden is a persistent but contained tax — if old patterns like func_get_args() or call_user_func_array() are used, each affected function pays the cost but the rest of the codebase is largely unaffected.

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

Closest to 'minor surprise' (t3), because the misconception states that developers think ...args is only for variadic function definitions, missing that spread (...) also works in function calls to unpack any iterable. This is a one-directional edge-case misunderstanding, not a catastrophic or deeply contradictory trap — just a partial mental model that omits the call-site unpacking usage.

About DEBT scoring →

TL;DR

PHP 5.6 added variadic functions (...$args) and argument unpacking ($array...) — replacing func_get_args() and call_user_func_array() with clean syntax.

Explanation

PHP 5.6: variadic params: function sum(int ...$nums) { return array_sum($nums); }. Spread in calls: sum(...[1,2,3]). Before PHP 5.6: func_get_args() and call_user_func_array(). PHP 7.4 extended spread to arrays: [...$a, ...$b]. PHP 8.1 extended to string-keyed arrays. Variadic params must be last in parameter list. Type-hinting variadics: function join(string ...$parts). Named arguments (PHP 8.0) interact with variadics. The spread operator in function calls unpacks arrays/iterables into positional arguments.

Common Misconception

...args is only for variadic functions — spread (...) in function calls unpacks any iterable as arguments, useful for calling any function with a dynamic argument list.

Why It Matters

Variadic functions and spread operators eliminate common func_get_args() boilerplate and make dynamic function calls clean and type-safe.

Common Mistakes

  • Putting variadic param before other params — must be last.
  • Not type-hinting variadic params.
  • Still using call_user_func_array instead of fn(...$args).

Code Examples

✗ Vulnerable
// PHP 5.5 and older:
function sum() {
    return array_sum(func_get_args());
}
call_user_func_array('sum', $numbers);
✓ Fixed
// PHP 5.6+:
function sum(int ...$nums): int {
    return array_sum($nums);
}
sum(1, 2, 3);
sum(...[1, 2, 3]); // Spread from array

// Forwarding all args:
function wrapper(...$args) {
    return original(...$args);
}

Added 23 Mar 2026
Views 22
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 7 ChatGPT 4 Google 4 Perplexity 2 Unknown AI 1 Meta AI 1 Ahrefs 1
crawler 18 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Replace func_get_args() with ...$params. Replace call_user_func_array($fn, $args) with $fn(...$args). Add type hints to variadic parameters.
📦 Applies To
PHP 5.6+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
func_get_args\(|call_user_func_array\(
Auto-detectable: ✓ Yes rector phpcs
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Function

✓ schema.org compliant