rfc:skipparams
no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


Previous revision
Next 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 <stas@php.net>
 +  * Status: Under Discussion
 +  * Implementation: https://github.com/smalyshev/php-src/tree/skip_params
  
 +===== Introduction =====
 +
 +As PHP does not have named parameter support, a 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. 
 +
 +===== 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("deleted=0", "name",,, /*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 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:
 +
 +       var_dump(2,,1);
 +
 +is the same as:
 +
 +       var_dump(2,1);
 +
 +===== 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:
 +
 +     function test($a=1, $b=2, $c=3) {
 +        var_dump(func_get_args());
 +     }
 +
 +     test(1,,4);
 +
 +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('test', array(42, 2=>43, 4=>44));
 +
 +call_user_func will allow argument skipping directly:
 +
 +      call_user_func('test', 42,,44);
 +
 +with the same result.
 +
 +===== User request examples =====
 +  * http://stackoverflow.com/q/579331/214196
 +  * http://stackoverflow.com/q/1115125/214196
 +  * http://stackoverflow.com/q/9541776/214196
 +  * http://stackoverflow.com/q/1066625/214196
 +  * http://stackoverflow.com/q/4453817/214196
 +  * http://stackoverflow.com/q/8356227/214196 (for Javascript)
 +  * http://stackoverflow.com/q/9888725/214196
 +  * http://stackoverflow.com/q/4681987/214196 (for Delphi)
 +  * http://stackoverflow.com/q/4435918/214196 (for AS)
 +
 +===== Issues raised =====
 +    * Using "default" instead of skipping parameter
 +    * 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