3

PHP code1:

function f() {return 'hello world!';}

echo f().'<br>';
echo f(123).'<br>';
echo f(123, 'foo').'<br>';

Output:

hello world!
hello world!
hello world!

 

PHP code2:

function g(int $x) {return 'hello world!';}

echo g(123).'<br>';
echo g(123, 'foo').'<br>';

Output:

hello world!
hello world!

Question

Is it possible to force PHP to throw an error when passing more arguments than the function expects?


Related Questions

  1. Why does PHP not throw an error when I pass too many parameters to a function?

 

Follow-up

There was already a RFC proposing a Strict Argument Count On Function Calls which, unfortunately, was withdraw due to high rejection. Some interesting points available in it are:

  • "During the tests it became clearly measurable that the proposed strict argument count check won't be an issue. Actually, it's quite the opposite. It will help to increase PHP code quality from PHP7 and forward as all warnings were useful to catch mistakes or even bugs."
  • "The RFC was withdraw due to many controversial points and overall rejection and won't be proposed again by the RFC author. The RFC author advises to not revive this RFC as it was already rejected."

A deeper discussion is also available: [PHP-DEV][RFC][DISCUSSION] Strict Argument Count

14
  • Check the return of func_num_args(), see if it matches what you expect, and if not, throw an error. Note that using a modern IDE will usually make it obvious when you are attempting to call a function with an improper number of arguments. Commented Oct 23, 2019 at 19:16
  • I think the problem is deciding how many arguments a function expects. If anything uses func_get_args() it becomes a case of having to work it out from the code. Commented Oct 23, 2019 at 19:20
  • @PatrickQ > "Check the return of func_num_args()" Including func_num_args in each single function is significantly painful. Is there any other approach more automatic? Commented Oct 23, 2019 at 19:42
  • @PatrickQ > "Note that using a modern IDE" Mind to recommend one of such IDE? Commented Oct 23, 2019 at 19:43
  • 1
    The post you link to has a link to bugs.php.net/bug.php?id=13892 which covers that point. Commented Oct 23, 2019 at 19:55

2 Answers 2

1

As others have suggested, use the ReflectionFunction class.

<?php
function foo($bar, $baz) {
    $refFunc = new ReflectionFunction(__FUNCTION__);
    if(func_num_args() > $refFunc->getNumberOfParameters())
        throw new Exception('Too many arguments.');
}

foo(1,2,3);

Will throw the exception.

Or simpler. Place this at the beginning of the function:

function bar($baz, $bat) {
    if(func_num_args()>count(get_defined_vars()))
        throw new Exception('Too many args.');
}

bar(1,2,3);

Will throw the exception.

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

4 Comments

Considering that I'm more familiar to procedural programming, the 2nd code seems much straight forward. However, still it is necessary to plug this snippet in every single function. Is there an approach more automatic? Ex: Is there an error level that automatically warns about too many arguments?
You get Php warnings for too few arguments. You could ape that with the one-liner: if(func_num_args()>count(get_defined_vars())) trigger_error('Too many arguments.', E_USER_WARNING);. I doubt there is an inbuilt that does this automagically.
I've added a link to a related RFC that was withdrawn: wiki.php.net/rfc/strict_argcount
Indeed, very interesting link. It is very curious to see the behavior of the developers. They concluded that such setting would "help to increase PHP code quality", however the proposal was withdraw due to overall rejection and the author advises not to revive it again. I would like to understand the reasoning behind that ...
0

In Php, it is not wrong to pass more arguments to a function than needed and you only get error if you pass fewer arguments.

Number of arguments passed to function can be obtained by func_num_args() but if you want to know how many parameters your function actually has, you can use reflection methods:

$method = new \ReflectionMethod('MyClassName', 'myFunction');
$method = new \ReflectionFunction('myFunction');
var_dump($method->getNumberOfParameters(), $method->getParameters());

1 Comment

The problem is that it would be very painful to include this in each single function. It would be nice if there were a solution more automatic.

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.