Named Arguments (PHP 8.0)
TL;DR
PHP 8.0 named arguments let you pass values by parameter name — skipping optional params, self-documenting calls, and enabling out-of-order argument passing.
Explanation
named_arg: value syntax at call sites: array_slice($arr, offset: 1, length: 3). Benefits: skip optional params with defaults, document complex calls, reorder args. Works with built-in functions too: htmlspecialchars($str, ENT_QUOTES | ENT_SUBSTITUTE, encoding: 'UTF-8'). In attributes: #[Route(path: '/home', methods: ['GET'])]. Named args cannot be combined with positional args in PHP 8.0 (fixed in 8.1). Spreading named args: fn(...$named). Cannot use named args for constructors with property promotion before 8.1. Named args in array_slice removed need to pass all earlier args.
Common Misconception
✗ Named arguments change how functions are implemented — they only affect call sites. The function signature is unchanged.
Why It Matters
Named arguments eliminate the need to remember parameter order for functions with many optional params — making complex built-in function calls self-documenting.
Common Mistakes
- Using named args for the first positional argument — only useful for optional params.
- Mixing positional and named args incorrectly (positional must come first).
- Not using named args in attributes where they dramatically improve readability.
Code Examples
✗ Vulnerable
array_slice($array, 1, 3, true); // What is true?
setcookie('session', $val, 0, '', '', true, true); // What are the booleans?
✓ Fixed
array_slice($array, offset: 1, length: 3, preserve_keys: true);
setcookie(
name: 'session',
value: $val,
secure: true,
httponly: true
);
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
20
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 8
Unknown AI 3
Perplexity 2
ChatGPT 1
Google 1
Ahrefs 1
Also referenced
How they use it
crawler 15
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Low
⚡ Quick Fix
Add named arguments to complex built-in calls (array_slice, setcookie, preg_replace). Always use named args in attribute constructors with multiple params.
📦 Applies To
PHP 8.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
setcookie\(|array_slice\(
Auto-detectable:
✗ No
rector
🤖 AI Agent
Confidence: Low
False Positives: High
✓ Auto-fixable
Fix: Low
Context: Line