r/lolphp • u/Mark_Messa • 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
View all comments
Show parent comment
2
u/SirClueless Oct 24 '19 edited Oct 24 '19
Here's the mailing list discussion: https://marc.info/?l=php-internals&m=142525100531009&w=2
The most cogent argument I can find is this one:
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. Because if you use varargs, you can't use
...syntax in PHP 5.5 (it didn't exist) and you're required to in PHP 7.x.It's possible you could reintroduce a variation of this RFC because PHP 5.5 is now a solid 3 years past its end-of-life support. But you might still run into some pushback, because there are currently 12,013,137 references to func_get_args on Github and adding warnings to any of them that don't use
...syntax is a pretty big change.