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

Named Arguments in Attributes

PHP PHP 8.1+ Intermediate
debt(d5/e1/b2/t3)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches' (d5). PHPStan (listed in detection_hints.tools) can detect misuse of attribute arguments, including type mismatches or invalid parameter names. However, the detection is not automatic by default — requires static analysis configuration. Basic syntax errors would be caught at parse time, but semantic issues like positional arg fragility require tooling.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch' (e1). The quick_fix states to simply use named arguments in attribute constructors — this is a single-line change per attribute usage. Converting #[Route('/path', 'GET')] to #[Route(path: '/path', methods: 'GET')] is a trivial inline modification.

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

Closest to 'minimal commitment' (b1), scored at b2. Named arguments in attributes are a local syntax choice affecting individual annotations. They don't create cross-cutting dependencies or architectural constraints. However, once a codebase adopts named args in attributes, there's mild consistency pressure to use them everywhere for uniformity, creating a small ongoing tax.

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

Closest to 'minor surprise' (t3). The misconception field indicates developers may expect special syntax for attribute named args when there is none — it's identical to regular named arguments. This is a small learning curve rather than a serious trap, as the syntax is consistent with PHP 8.0+ named arguments elsewhere. The edge case around PHP version requirements (8.0+ for named args, 8.1+ for attributes with named args) adds minor confusion.

About DEBT scoring →

TL;DR

PHP 8.1+ allows named arguments inside attribute constructors — #[Route(path: '/home', methods: ['GET'])] — making attribute usage self-documenting.

Explanation

PHP attributes (#[AttributeName]) can accept constructor arguments. With named arguments (PHP 8.0+), attribute arguments can be passed by name rather than position, making complex attributes readable. This is especially useful for framework attributes with many optional parameters. Attribute classes are defined with #[Attribute] and a regular constructor. Named args in attributes follow the same rules as named args in regular function calls — you can reorder, skip optional ones, and the code is self-documenting.

Common Misconception

Named arguments in attributes require a special syntax — they use the same fn(param: value) syntax as regular named arguments in PHP 8.0+.

Why It Matters

Named arguments in attributes eliminate positional argument confusion in complex metadata like route definitions, validation rules, and ORM mappings.

Common Mistakes

  • Using positional args in attributes with many parameters — order-sensitive and fragile.
  • Forgetting attribute class must be declared with #[Attribute] and has a constructor.
  • Using named args for attributes in PHP < 8.0 — named args require 8.0.

Code Examples

✗ Vulnerable
#[Route('/home', ['GET'], true, 'home_route')]
class HomeController {}
✓ Fixed
#[Route(
    path: '/home',
    methods: ['GET'],
    requiresAuth: false,
    name: 'home_route'
)]
class HomeController {}

Added 23 Mar 2026
Views 69
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
2 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 2 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 2 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M
No pings yet today
SEMrush 1
Amazonbot 9 SEMrush 6 PetalBot 5 ChatGPT 4 Ahrefs 4 Scrapy 4 Unknown AI 3 Perplexity 3 Google 3 Meta AI 2 Twitter/X 1 Brave Search 1 Applebot 1 Bing 1
crawler 43 crawler_json 3 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
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Use named arguments in all attribute constructors with more than 2 parameters for self-documenting metadata.
📦 Applies To
PHP 8.1+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
#\[
Auto-detectable: ✗ No phpstan
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant