rfc:skipparams
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | rfc:skipparams [2012/05/11 23:15] – [Issues raised] stas | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Skipping optional parameters for functions ====== | ||
+ | * Version: 1.0 | ||
+ | * Date: 2012-04-13 | ||
+ | * Author: Stas Malyshev < | ||
+ | * Status: Under Discussion | ||
+ | * Implementation: | ||
+ | ===== Introduction ===== | ||
+ | |||
+ | As PHP does not have named parameter support, a very common for function is to have many optional arguments, like this: | ||
+ | |||
+ | | ||
+ | {...} | ||
+ | |||
+ | 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. | ||
+ | |||
+ | ===== Proposal ===== | ||
+ | |||
+ | 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(" | ||
+ | |||
+ | 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 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 parameters. | ||
+ | |||
+ | ===== Implementation ===== | ||
+ | |||
+ | On the engine level, it will be implemented by putting NULL in the place where the parameter is passed. Functions dealing with argument handling will be updated. | ||
+ | |||
+ | See example implementation above. Tests for most use cases will be added shortly. | ||
+ | |||
+ | ===== 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 NULLs, thus leaving the defaults supplied by the caller intact. Again, skipping non-optional parameter is an error. | ||
+ | |||
+ | Variadic functions will not return skipped parameters in argc and argv, effectively ignoring them, so: | ||
+ | |||
+ | | ||
+ | |||
+ | is the same as: | ||
+ | |||
+ | | ||
+ | |||
+ | ===== func_get_args() ===== | ||
+ | |||
+ | func_get_args() will skip parameters that are not supplied, so it would provide just the parameters that are actually supplied to the function. Thus, this code: | ||
+ | |||
+ | | ||
+ | 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 actually passed. | ||
+ | func_get_arg() will return false for parameter that was not passed. | ||
+ | |||
+ | ===== call_user_func_array() ===== | ||
+ | |||
+ | call_user_func_array() will allow skipping arguments too by skipping them in supplied arguments array, like this: | ||
+ | |||
+ | call_user_func_array(' | ||
+ | |||
+ | call_user_func will allow argument skipping directly: | ||
+ | |||
+ | call_user_func(' | ||
+ | |||
+ | with the same result. | ||
+ | |||
+ | ===== User request examples ===== | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | |||
+ | ===== Issues raised ===== | ||
+ | * Using " | ||
+ | * Support for internal functions doing manual ZEND_NUM_ARGS() | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * 2012-04-13 First draft. |
rfc/skipparams.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1