rfc:too_few_args

This is an old revision of the document!


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

  • Version: 0.9
  • Date: 2016-06-01
  • Author: Dmitry Stogov, dmitry@zend.com
  • Status: Draft (or Under Discussion or Accepted or Declined)

Introduction

Historically, PHP allows calling functions with fewer actual parameters when required by 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: Undefind variable: a 
   var_dump($a);   // NULL + Warning: Undefind 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 optimisation opportunists

Proposal

I propose to disable calling functions with insufficient actual parameters. PHP will throw a “Error” exception insted.

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 function with unexpected input data are going to be caught in first place.

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.

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 git commit(s)
  3. a link to the PHP manual entry for the feature
rfc/too_few_args.1464778137.txt.gz · Last modified: 2017/09/22 13:28 (external edit)