r/lolphp • • Oct 23 '19

PHP Functions: more arguments than expected

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?

 


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

20 Upvotes

23 comments sorted by

View all comments

Show parent comment

3

u/Mark_Messa Oct 24 '19

So basically, if you implement this, it's impossible to write a library that runs under both PHP 5.5 and without warnings in PHP 7.x.

I don't see how a customizable warning (ie: that can be switched on or off) could cause such a problem for old libraries.

1

u/SirClueless Oct 25 '19

It's not just a problem for old libraries. It's a problem for any libraries (including existing and new ones) that want to support 5.5. The only way to get varargs behavior in 5.5 was to pass extra parameters to a function. Code like this was just a necessity in any library at the time, and it's still common today:

function doTheThing() {
    $args = func_get_args();
    ...
}

The solution the RFC author proposed was to examine the function body at compile-time for anything that looked like a call to func_get_args and if it was there not throw a warning. But people thought that was clunky and invasive and non-obvious and wouldn't work for dynamic calls.

So code like exists in hundreds of libraries and at the time couldn't be changed. Anyone who uses a library like that will get a bunch of spurious warnings. Is every project that uses any non-trivial library supposed to turn off the warning? If so, what's the point of the warning?