26

I am a n00b at php. I was learning about Default Parameters so I made this function.

function doFoo($name = "johnny"){
    echo "Hello $name" . "<br />";
}

I made these calls

doFoo();
doFoo("ted");
doFoo("ted", 22);

The first two printed what was expected i.e

Hello johnny
Hello ted

but the third call also printed

Hello ted

I was expecting an error, after all the function is made for one argument whereas I am calling it with two arguments.
Why was there no error?

1

5 Answers 5

18

PHP doesn't throw an error on function overload.

1
  • 9
    I am sorry, but this is a really poor answer. Function/method overloading means having multiple methods with the same name but with different parameter signatures. It DOES NOT mean accepting a call to a method with too many parameters and just dropping off the extra parameters that don't fit the signature. The reason PHP accepts more parameters than called for in a method is a fundamental flaw in the language, not a result of any recognized programming syntactical practice.
    – Mark C.
    Commented Jan 3, 2020 at 0:39
9

It is not wrong to pass more arguments to a function than needed.

You only get error if you pass to few arguments.

function test($arg1) {
 var_dump($arg1);
}

test();

Above will result in following error:
Uncaught ArgumentCountError: Too few arguments to function...

If you want to fetch first argument plus all others arguments passed to function you can do:

function test($arg1, ...$args) {
 var_dump($arg1, $args);
}

test('test1', 'test2', 'test3');

Resulting in:
string(5) "test1" array(2) { [0]=> string(5) "test2" [1]=> string(5) "test3" }

2
  • This is wrong, at least in 2021. Passing more than the expected args to a function throws an error. See this example on intval() when passing more than 2 args: PHP Warning: intval() expects at most 2 parameters, 4 given in /path/to/file.php on line XXX
    – user3934058
    Commented Jul 30, 2021 at 5:26
  • 2
    Re the comment above. It seems that many (all?? I didn't test them all!) built-in PHP functions will error if you pass too many arguments to them, but user-defined functions don't - demo: 3v4l.org/UQAfO
    – ADyson
    Commented Jul 27 at 22:59
7

because PHP functions support variable number of parameters.

0

Apparently because that's how PHP programmers like it. There was a String Argument Count RFC to emit a Notice when too many arguments are sent, but it was soundly rejected, to my dismay: https://wiki.php.net/rfc/strict_argcount

-8

It should only print a notice, but no error. I think you have your error reporting set up so that notices are not shown on screen.

Try pasting this at the top of your code:

error_reporting(E_ALL | E_STRICT);
2
  • 11
    As far as I can see, this is incorrect. Even when setting the strictest error reporting setting there will be no notices.
    – arvidj
    Commented Nov 24, 2014 at 16:43
  • This is not a wrong answer. Since the error is a warning, very well the error reporting statement does influence the visible error. This is in fact the probable right answer. Upvoted.
    – user3934058
    Commented Jul 30, 2021 at 5:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.