rfc:skipparams

Differences

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

Link to this comparison view

rfc:skipparams [2012/07/08 09:26]
stas [Changelog]
rfc:skipparams [2013/09/02 23:46] (current)
stas [Changelog]
Line 1: Line 1:
 ====== Skipping optional parameters for functions ====== ====== Skipping optional parameters for functions ======
-  * Version: ​1.0 +  * Version: ​2.0 
-  * Date: 2012-04-13+  * Date: 2013-09-01
   * Author: Stas Malyshev <​stas@php.net>​   * Author: Stas Malyshev <​stas@php.net>​
   * Status: Under Discussion   * Status: Under Discussion
-  * Implementation:​ https://​github.com/​smalyshev/​php-src/​tree/​skip_params +  * Implementation: ​https://​github.com/​php/​php-src/​pull/​426, ​https://​github.com/​smalyshev/​php-src/​tree/​skip_params
 ===== Introduction ===== ===== Introduction =====
  
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 ​parameters.+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 enough ​parameters.
  
 ===== Implementation ===== ===== Implementation =====
Line 30: Line 29:
 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. 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.+See example implementation above. ​See tests for examples of most common uses cases. ​
  
 ===== User functions ===== ===== User functions =====
Line 82: Line 81:
  
 with the same result. with the same result.
 +
 +===== Internal API changes =====
 +Parameters stored as zval** in the engine. For skipped parameter, the value stored is NULL instead of zval* to specific argument. The opcode putting NULL on the stack is implemented as znode type IS_UNUSED.
 +
 +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 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. ​
 +
 +For variadic parameters like + and *, skipped parameters will be ignored and not included in the list as if they were not passed. In other words, + and * will never return NULLs among parameters. ​
 +
 +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 "​Parameter %d missing"​ if any skipped parameters are passed (i.e. would never return NULLs as parameters).
 +
 +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. 
  
 ===== User request examples ===== ===== User request examples =====
Line 93: Line 103:
   * http://​stackoverflow.com/​q/​4681987/​214196 (for Delphi)   * http://​stackoverflow.com/​q/​4681987/​214196 (for Delphi)
   * http://​stackoverflow.com/​q/​4435918/​214196 (for AS)   * http://​stackoverflow.com/​q/​4435918/​214196 (for AS)
 +  * http://​stackoverflow.com/​q/​812058/​214196 (for Ruby)
 ===== Issues raised ===== ===== Issues raised =====
-    * Support for internal functions doing manual ZEND_NUM_ARGS()+    * Support for internal functions doing manual ZEND_NUM_ARGS(). I've looked into all functions in the standard distribution,​ but PECL extensions that are not using zend_parse_parameters or do weird things with zval-type ones without properly initializing them or checking the output results, may require fixing, otherwise they may crash if "​default"​ is passed to them due to null pointer reference. This crash doesn'​t seem to be dangerous security-wise,​ but would of course be unpleasant. ​
  
 ===== Changelog ===== ===== Changelog =====
Line 101: Line 111:
   * 2012-04-13 First draft.   * 2012-04-13 First draft.
   * 2012-07-07 Changed empty parameter to use '​default'​   * 2012-07-07 Changed empty parameter to use '​default'​
 +  * 2013-09-01 Added Zend API description
rfc/skipparams.1341732387.txt.gz · Last modified: 2012/07/08 09:26 by stas