PHP 8.1. I declared a method with this signature:
public static function if(mixed $condition, mixed $subject = null, \Closure $callback)
And called it with this call:
CLASS::if(condition: is_numeric(...), callback: do_something(...));
Yet I still got this exception:
ArgumentCountError: CLASS::if(): Argument #2 ($subject) not passed
Why? $subject
clearly is set to default to null.
Since the $callback
parameter has no default value, it's a required argument, and so is every argument before it. Therefore, $subject = null
doesn't mean null
is a default value; it only means $subject
is nullable (everything other than null
would have triggered a deprecation notice).
To solve the problem, depending on the behaviour you're expecting, you can either place the $subject
parameter at the end, or add a default value to the $callback
parameter.