rfc:too_few_args

PHP RFC: Replace "Missing argument" warning with "Too few arguments" exception

Introduction

Historically, PHP allows calling functions with fewer actual parameters than required by the function definition. These “non-passed” arguments lead to warning emission and continuation of function execution with uninitialized arguments.

function foo($a) {
   var_dump($a);   // NULL + Warning: Undefined variable: a 
   var_dump($a);   // NULL + Warning: Undefined variable: a
}
foo();             // Warning: Missing argument 1 for foo()

This strange behavior:

  • allows execution of functions with unexpected input data (nobody checks isset() for all arguments)
  • doesn't have real use cases (in any case, foo($a = null) is better)
  • may lead to warning bloating
  • disables obvious optimization opportunities

Proposal

I propose to disable calling “user” functions with insufficient actual parameters. PHP will throw an “Error” exception instead.

function foo($a) {
   var_dump($a);   // not executed
   var_dump($a);   // not executed
}
foo();             // throw Error("Too few arguments to function foo(), 0 passed in %s on line %d and exactly 1 expected")

Using this approach, all attempts to call functions with unexpected input data are going to be caught as soon as possible.

Behavior of internal functions is not going to be changed.

Backward Incompatible Changes

The BC break in intended.

Proposed PHP Version(s)

PHP 7.1

Proposed Voting Choices

The vote is a straight Yes/No vote, that requires a 2/3 majority. The voting began on Jun 6 and will close on Jun 16.

Replace
Real name Yes No
aharvey (aharvey)  
ajf (ajf)  
bishop (bishop)  
bwoebi (bwoebi)  
cmb (cmb)  
colinodell (colinodell)  
danack (danack)  
daverandom (daverandom)  
davey (davey)  
derick (derick)  
dm (dm)  
dmitry (dmitry)  
dragoonis (dragoonis)  
fa (fa)  
galvao (galvao)  
guilhermeblanco (guilhermeblanco)  
hywan (hywan)  
kalle (kalle)  
kguest (kguest)  
krakjoe (krakjoe)  
laruence (laruence)  
lcobucci (lcobucci)  
leigh (leigh)  
levim (levim)  
lstrojny (lstrojny)  
marcio (marcio)  
mariano (mariano)  
mattwil (mattwil)  
mcmic (mcmic)  
mrook (mrook)  
nikic (nikic)  
ocramius (ocramius)  
pajoye (pajoye)  
patrickallaert (patrickallaert)  
peehaa (peehaa)  
pierrick (pierrick)  
pmjones (pmjones)  
pollita (pollita)  
rasmus (rasmus)  
sammyk (sammyk)  
santiagolizardo (santiagolizardo)  
sebastian (sebastian)  
sobak (sobak)  
stas (stas)  
thekid (thekid)  
tpunt (tpunt)  
trowski (trowski)  
yohgaki (yohgaki)  
zeev (zeev)  
zimt (zimt)  
Final result: 39 11
This poll has been closed.

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the PHP manual entry for the feature
rfc/too_few_args.txt · Last modified: 2016/06/16 07:55 by dmitry