ArgumentCountError — Wrong Arg Count
TL;DR
PHP 7.1+ throws ArgumentCountError when too few arguments are passed to a function — too many arguments are silently ignored (unless using strict_types).
Explanation
ArgumentCountError extends TypeError extends Error. It's thrown when a function requiring N parameters receives fewer. Too many arguments is only an error in strict mode or with variadic functions. Common scenarios: refactoring a function to require more parameters breaks all callers, calling a method with wrong interface signature. In PHP 8, named arguments allow skipping positional requirements but still enforce parameter count. Check with func_num_args() in legacy variadic code.
Common Misconception
✗ Passing extra arguments always throws an error — PHP silently ignores extra positional arguments unless the function uses ... variadic syntax.
Why It Matters
ArgumentCountError surfaces interface contract violations immediately at the call site rather than creating undefined behaviour inside the function.
Common Mistakes
- Adding a required parameter to an existing function without updating all callers.
- Not using default values for new optional parameters.
- Calling parent::__construct() with wrong argument count in subclasses.
Code Examples
✗ Vulnerable
function createUser(string $name, string $email, string $role): User {
// ...
}
createUser('Paul', 'paul@example.com');
// ArgumentCountError: Too few arguments (2), 3 required
✓ Fixed
// Add with a default value to avoid breaking callers:
function createUser(string $name, string $email, string $role = 'user'): User {
// ...
}
// Or use named arguments (PHP 8):
createUser(name: 'Paul', email: 'paul@example.com', role: 'admin');
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
22 Mar 2026
Views
21
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
Amazonbot 6
Perplexity 4
Unknown AI 3
Google 2
ChatGPT 1
Ahrefs 1
Also referenced
How they use it
crawler 15
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Low
⚡ Quick Fix
Add default values to new parameters to avoid breaking callers. Use PHP 8 named arguments for clarity. Catch \ArgumentCountError or \TypeError when calling dynamic callables.
📦 Applies To
PHP 7.1+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
ArgumentCountError
Auto-detectable:
✓ Yes
phpstan
psalm
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Low
✗ Manual fix
Fix: Low
Context: Function
Tests: Update
CWE-252