dmitry

Dmitry Stogov , from Zend Technologies
(user votes do not necessarily reflect their company's views)

Contents

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

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

Votes

An option needs 2/3 votes to win

Replace (78% approved)
User Vote
aharvey No
ajf No
bishop No
bwoebi Yes
cmb No
colinodell Yes
danack Yes
daverandom Yes
davey No
derick No
dm Yes
dmitry Yes
dragoonis Yes
fa Yes
galvao Yes
guilhermeblanco Yes
hywan Yes
kalle Yes
kguest No
krakjoe Yes
laruence Yes
lcobucci Yes
leigh Yes
levim Yes
lstrojny Yes
marcio Yes
mariano Yes
mattwil Yes
mcmic Yes
mrook Yes
nikic Yes
ocramius Yes
pajoye No
patrickallaert No
peehaa Yes
pierrick Yes
pmjones Yes
pollita Yes
rasmus Yes
sammyk Yes
santiagolizardo Yes
sebastian No
sobak Yes
stas Yes
thekid Yes
tpunt Yes
trowski Yes
yohgaki Yes
zeev Yes
zimt No