rfc:skipparams
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:skipparams [2013/09/02 05:57] – [Internal API changes] stas | rfc:skipparams [2015/02/22 20:53] – stas | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Skipping optional parameters for functions ====== | ====== Skipping optional parameters for functions ====== | ||
- | * Version: | + | * Version: |
- | * Date: 2013-09-01 | + | * Date: 2015-01-01 |
* Author: Stas Malyshev < | * Author: Stas Malyshev < | ||
- | * Status: | + | * Status: |
- | * Implementation: | + | * Implementation: |
===== Introduction ===== | ===== Introduction ===== | ||
- | As PHP does not have named parameter support, a very common for function is to have many optional arguments, like this: | + | In PHP, it is very common for function is to have many optional arguments, like this: |
| | ||
Line 24: | Line 23: | ||
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. | 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 optional parameters can be skipped this way, skipping non-optional one will produce the same error it does now when function is not given engough | + | Only declared |
===== Implementation ===== | ===== Implementation ===== | ||
- | On the engine level, it will be implemented by putting | + | On the engine level, it will be implemented by putting |
- | See example implementation | + | See example implementation |
+ | |||
+ | See tests there for examples of most common uses cases. | ||
===== User functions ===== | ===== User functions ===== | ||
Line 38: | Line 39: | ||
===== Internal functions ===== | ===== Internal functions ===== | ||
- | For internal functions, parameter parser will ignore the NULLs, thus leaving the defaults supplied by the caller intact. Again, skipping non-optional parameter is an error. | + | 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. |
- | Variadic functions will not return skipped parameters in argc and argv, effectively ignoring them, so: | + | Thus, this code is an error: |
| | ||
- | is the same as: | + | but this is not: |
- | var_dump(2,1); | + | call_user_func(' |
+ | |||
+ | since call_user_func is specifically described as accepting defaults. | ||
===== func_get_args() ===== | ===== func_get_args() ===== | ||
- | func_get_args() will skip parameters that are not supplied, | + | func_get_args() will use default values for parameters that are not supplied. Because of how PHP 7 engine works, it is not possible |
- | + | ||
- | | + | |
- | var_dump(func_get_args()); | + | |
- | } | + | |
- | + | ||
- | | + | |
- | + | ||
- | will produce: | + | |
- | + | ||
- | array(2) { | + | |
- | [0]=> | + | |
- | int(1) | + | |
- | [2]=> | + | |
- | int(4) | + | |
- | } | + | |
- | + | ||
- | This means that if you are looping over func_get_args() result, you should use foreach() and not counter-based for(). | + | |
- | func_num_args() will also ignore skipped parameters and give only the number of parameters | + | |
- | func_get_arg() will return false for parameter that was not passed. | + | |
===== call_user_func_array() ===== | ===== call_user_func_array() ===== | ||
- | call_user_func_array() will allow skipping | + | Currently, |
- | + | ||
- | call_user_func_array(' | + | |
- | + | ||
- | call_user_func | + | |
- | + | ||
- | call_user_func(' | + | |
- | + | ||
- | with the same result. | + | |
===== Internal API changes ===== | ===== Internal API changes ===== | ||
- | Parameters stored as zval** in the engine. For skipped parameter, the value stored is NULL instead of zval* to specific argument. | + | Parameters stored as array of zvals n the engine. For skipped parameter, the zval type is stored |
- | + | ||
- | zend_parse_parameter() would ignore parameters marked as default - meaning, it will not assign any value to the underlying variable. This is unless the parameter is marked | + | |
- | For variadic | + | zend_parse_parameter() would ignore |
- | If certain function wants to disallow skipping parameters, it should use option ZEND_PARSE_PARAMS_NODEFAULT with zend_parse_parameters_ex(). In this case, skipped parameters will cause . For older APIs, zend_get_parameters_array_nodefault() is available which will fail with E_WARNING " | + | 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. But trying to get a skipped parameter with API functions will produce NULL. | + | ZEND_NUM_ARGS() is always the number of parameters in function call, so skipped parameters are counted there. |
===== User request examples ===== | ===== User request examples ===== | ||
Line 104: | Line 78: | ||
* http:// | * http:// | ||
* http:// | * http:// | ||
+ | * http:// | ||
===== Issues raised ===== | ===== Issues raised ===== | ||
- | | + | |
+ | * Internal | ||
+ | |||
+ | * 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, | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | Since this RFC changes the language semantics, the 2/3+1 vote majority is required for it to pass. The vote is a straight Yes/No vote. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | The vote concludes on the end of the day, PST, February 21th. | ||
===== Changelog ===== | ===== Changelog ===== | ||
Line 112: | Line 100: | ||
* 2012-04-13 First draft. | * 2012-04-13 First draft. | ||
* 2012-07-07 Changed empty parameter to use ' | * 2012-07-07 Changed empty parameter to use ' | ||
- | * Added Zend API description | + | * 2013-09-01 |
+ | * 2015-01-01 Updated for PHPNG |
rfc/skipparams.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1