0

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.

2

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you feel that one of the links that I commented is a suitable dupe target, please vote to close and transfer your advice there because stackoverflow.com/help/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.