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
$callbackparameter has no default value, it's a required argument, and so is every argument before it. Therefore,$subject = nulldoesn't meannullis a default value; it only means$subjectis nullable (everything other thannullwould have triggered a deprecation notice).To solve the problem, depending on the behaviour you're expecting, you can either place the
$subjectparameter at the end, or add a default value to the$callbackparameter.