Reputation: 1112
In php you can usually call a function with more parameters than have been defined, e.g.
function f($a, $b) {
return $a . $b;
}
f('a', 'b', 'c', 'e');
is totally valid.
I understand that you can define functions with a variable number of parameters. But in most cases that is not what you want to do.
So in these cases if you mistakenly replace a .
by a ,
you will not even get warning.
$x = '0';
f('a' , 'b' . $x . 'c') // returns ab0c
f('a' , 'b' , $x . 'c') // returns ab
Now in PHP 7 there is the function f($a, $b, ...$others)
syntax. So in princable it is possible to discern variable parameter functions from ordinary ones.
Is there a way to get a notice when a function is called with too many parameters?
Upvotes: 1
Views: 109
Reputation: 851
There is a method of actually counting the variables sent to your function, however you can only do this INSIDE the function or BEFORE the function by counting the sent items.
See http://php.net/manual/en/function.func-num-args.php for more information.
You can also use http://php.net/manual/en/function.func-get-args.php to GET the current arguments sent to the function.
Your issue would however need something like this:
function(){
$count = func_num_args();
if($count > 3){
//too many args
}
else{
//continue your actual code
}
}
Upvotes: 1