rfc:zpp-conversion-rules
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:zpp-conversion-rules [2015/02/19 04:44] – francois | rfc:zpp-conversion-rules [2015/02/22 01:00] – old revision restored (2015/02/20 07:52) francois | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Type parsing | + | ====== PHP RFC: Type parsing |
- | * Version: 0.1 | + | * Version: 0.4 |
- | * Date: 2015-02-19 | + | * Date: 2015-02-22 |
* Author: François Laupretre, francois@php.net | * Author: François Laupretre, francois@php.net | ||
* Status: Draft | * Status: Draft | ||
Line 9: | Line 9: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RFC proposes | + | This RFC proposes |
- | internal function | + | and convert input arguments |
+ | |||
+ | ===== History ===== | ||
The changes proposed here are based on concerns that arose during the discussion about | The changes proposed here are based on concerns that arose during the discussion about | ||
[[https:// | [[https:// | ||
- | While the discussion | + | While the discussion |
- | about so-called | + | each camp showing use cases to prove it was right, we decided to gather all these use cases |
- | a compromise that would satisfy both camps. | + | and go exploring another way : search |
It was soon clear that the strongest argument of strict-typing proponents was that | It was soon clear that the strongest argument of strict-typing proponents was that | ||
- | ' | + | ' |
- | allowing, for instance, true or '7 years' as integer value. | + | |
- | + | ||
- | As the proposed argument type hinting mechanism is aligned on the ZPP behavior, | + | |
- | and as everyone seemed to agree it should remain so, it was decided to propose a new more | + | |
- | restrictive ruleset to be implemented in ZPP and zend_parse_parameters(). | + | |
- | So, this RFC presents | + | As the ' |
+ | and as everyone agreed any single-mode mechanism should keep using it, | ||
+ | we decided to propose | ||
- | Note: In the rest of the document, ' | + | Note: In the whole document, |
- | as, whatever the final implementation, both mechanisms must remain in sync. | + | as, whatever the ruleset, these mechanisms must keep implementing the same logic. |
===== Proposal ===== | ===== Proposal ===== | ||
Line 43: | Line 42: | ||
The following table shows the current rules used to accept and convert an input zval through | The following table shows the current rules used to accept and convert an input zval through | ||
- | the ZPP layer : | + | ZPP : |
^ ^ Zval type ^^^^^^^^ | ^ ^ Zval type ^^^^^^^^ | ||
^ ZPP type ^ IS_NULL | ^ ZPP type ^ IS_NULL | ||
^ bool | Yes (-> false) | < | ^ bool | Yes (-> false) | < | ||
- | ^ long | Yes (-> 0) | Yes (-> 0) | Yes (-> 1) | < | + | ^ long | Yes (-> 0) | Yes (-> 0) | Yes (-> 1) | < |
^ double | ^ double | ||
^ string | ^ string | ||
Line 56: | Line 55: | ||
* (3) Run string through is_numeric_str_function() and detect error. If double returned, convert it to long | * (3) Run string through is_numeric_str_function() and detect error. If double returned, convert it to long | ||
* (4) Run string through is_numeric_str_function() and detect error. If int returned, convert it to double | * (4) Run string through is_numeric_str_function() and detect error. If int returned, convert it to double | ||
+ | * (5) Discard fractional part, if any | ||
The conversion of IS_STRING to int/float is done through _is_numeric_string_ex(). | The conversion of IS_STRING to int/float is done through _is_numeric_string_ex(). | ||
Among others, this function has the following behavior : | Among others, this function has the following behavior : | ||
- | * Stop conversion at first non digit character with no error (this is what authorizes '7 years' as integer). | + | * Stop conversion at first non digit character with no error (ignore every trailing char). |
==== Proposed changes ==== | ==== Proposed changes ==== | ||
- | * Disable IS_FALSE/ | + | * Disable IS_FALSE/ |
- | * Disable (IS_STRING | + | * IS_STRING |
- | * Disable IS_NULL | + | * Disable IS_NULL to be accepted for any ZPP type except |
* Disable (IS_DOUBLE -> long) if the fractional part of the input value is non null. | * Disable (IS_DOUBLE -> long) if the fractional part of the input value is non null. | ||
* When converting from IS_STRING to long, reject conversion if string contains the representation of a number with a non null fractional part. | * When converting from IS_STRING to long, reject conversion if string contains the representation of a number with a non null fractional part. | ||
+ | * Add a ' | ||
In _is_numeric_string_ex() : | In _is_numeric_string_ex() : | ||
Line 79: | Line 80: | ||
^ ^ Zval type ^^^^^^^^ | ^ ^ Zval type ^^^^^^^^ | ||
- | ^ ZPP type ^ IS_NULL ^ IS_FALSE ^ IS_TRUE | + | ^ ZPP type ^ IS_NULL |
- | ^ bool | - | < | + | ^ null | < |
- | ^ long | - | - | + | ^ bool | - |
- | ^ double | + | ^ long | - |
- | ^ string | + | ^ double |
+ | ^ string | ||
* (1) false if null, true if non null | * (1) false if null, true if non null | ||
Line 89: | Line 91: | ||
* (3) Run string through is_numeric_str_function() and detect error. If double with null fractional part returned, convert to long, else error | * (3) Run string through is_numeric_str_function() and detect error. If double with null fractional part returned, convert to long, else error | ||
* (4) Run string through is_numeric_str_function() and detect error. If int returned, convert to double | * (4) Run string through is_numeric_str_function() and detect error. If int returned, convert to double | ||
+ | * (5) If string is numeric and contains a representation of a null number (anything that would convert to 0 or 0.0), or if string is empty, -> false, otherwise -> true. | ||
and the new behavior of _is_numeric_string_ex() : | and the new behavior of _is_numeric_string_ex() : | ||
Line 97: | Line 100: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | Every restriction of the ruleset causes | + | Every change we propose here is a BC break. |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
- | Because of BC breaks, requires a major version, so 7.0. | + | 7.0. |
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Line 108: | Line 111: | ||
Any code converting a string to a number is potentially impacted. If this cause | Any code converting a string to a number is potentially impacted. If this cause | ||
- | an unacceptable BC break, we'll create a private copy of this function | + | an unacceptable BC break, we'll create a private copy of the function |
- | for argument parsing. As long as it is not needed, | + | (string -> int/float) and will reserve it for ZPP. As long as it is not clearly |
- | numeric conversion uses the same code. | + | we'll keep a common code for every string to number conversions in PHP. |
==== To Opcache ==== | ==== To Opcache ==== | ||
Line 121: | Line 124: | ||
==== php.ini Defaults ==== | ==== php.ini Defaults ==== | ||
- | |||
- | None | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | |||
- | Coming soon... | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
Line 132: | Line 131: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | The set of supported numeric strings can be extended. | + | String to numeric conversion can be improved |
- | + | ||
- | String to numeric conversion can be made smarter | + | |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== |
rfc/zpp-conversion-rules.txt · Last modified: 2021/03/27 14:25 by ilutov