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


Skipping optional parameters for functions


In PHP, it is very common for function is to have many optional arguments, like this:

   function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true)

If we always use defaults, it's fine. But what if we need ot change $report_errors but don't care about the others? We'd have to find function definition and copy-paste all other defaults into the call, which is annoying, error-prone and may not do what you wanted if some of the defaults change.


The proposal is to allow skipping optional arguments in a call, thus making them assume default values as they do when they are not provided, like this:

    create_query("deleted=0", "name", default, default, /*report_errors*/ true);

This means that $join_type and $execute are going to use defaults. Of course, if we ever get implementation of named parameters, it may also solve this problem, but until we do, this can be a partial solution.

Only declared optional parameters can be skipped this way, skipping non-optional one will produce the same error it does now when function is not given enough parameters. Also, for variadic functions, variadic parameters can not be skipped - only parameters that are explicitly declared and marked optional can be skipped.


On the engine level, it will be implemented by putting IS_UNDEF value in the place where the parameter is passed. Functions dealing with argument handling will be updated.

See example implementation at:

See tests there for examples of most common uses cases.

User functions

User functions would have defaults for optional args take place of skipped arguments, just as before.

Internal functions

For internal functions, parameter parser will ignore the skipped parameters, thus leaving the defaults supplied by the caller intact. Again, skipping non-optional parameter is an error. For variadic parameters, as per above, skipping is not allowed, unless specifically requested by function declaration.

Thus, this code is an error:


but this is not:

     call_user_func('foo', 2, default, 1);

since call_user_func is specifically described as accepting defaults.


func_get_args() will use default values for parameters that are not supplied. Because of how PHP 7 engine works, it is not possible to distinguish skipped parameter from parameter where default value is actually passed.


Currently, call_user_func_array does not support skipping parameters due to the fact that previously it accepted any array as parameters list, but supporting skipping parameters would mean only sequential indexed numeric array will be accepted.

Internal API changes

Parameters stored as array of zvals n the engine. For skipped parameter, the zval type is stored as IS_UNDEF.

zend_parse_parameter() would ignore parameters marked as skipped/default - meaning, it will not assign any value to the underlying variable. This is unless the parameter is marked as !, in which case the parameter would be nullified just as if null were passed. This means you can not have variables marked as ! with different behavior between null and 'default', but I did not find any such cases to be required.

If certain function wants to disallow skipping parameters, it should use option ZEND_PARSE_PARAMS_NODEFAULT with zend_parse_parameters_ex(). This may be when internal function has optional arguments but does not have any defaults for them. In this case, skipped parameters will cause a catchable fatal error.

ZEND_NUM_ARGS() is always the number of parameters in function call, so skipped parameters are counted there.

User request examples

Issues raised

* Internal functions that declare parameters as optional but fail to provide proper defaults and rely on ZEND_NUM_ARGS to figure out if to use default or not may be broken. The patch fixes all instances of this in the core extensions, but third-party extensions may need to be fixed too. This applies only to ones that check ZEND_NUM_ARGS() manually in the code instead of using zend_parse_parameters().

* This RFC does not prevent named parameters implementation - in fact, a lot of cleanup to the code mentioned above is also necessary for named parameters implementation, since it would require the same level of care with providing the defaults. Both features can be used in parallel, and thus this RFC is a complimentary functionality for potential named parameters implementation.


Version Changed Date
4 First draft.
3 Changed empty parameter to use 'default'
2 Added Zend API description
1 Updated for PHPNG


An option needs 2/3 votes to win

Should PHP 7 support parameter skipping as described in this RFC? (38.6% approved)
User Vote
aharvey Yes
ajf No
auroraeosrose No
ben No
bishop Yes
brandon No
crodas No
danack Yes
daverandom Yes
dm No
dmitry No
galvao No
guilhermeblanco No
indeyets Yes
irker Yes
jedibc No
jgmdev No
jwage No
kalle No
kinncj No
lcobucci No
leigh No
levim No
lstrojny No
marco Yes
mbeccati Yes
mfischer No
mike Yes
mrook No
nikic No
pauloelr No
philstu No
pollita Yes
ramsey No
rdlowrey No
salathe No
stas Yes
subjective Yes
thekid Yes
toby Yes
treffynnon No
tyrael Yes
yohgaki Yes
yunosh Yes