rfc:parameter_type_casting_hints
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:parameter_type_casting_hints [2012/03/04 00:38] – created ircmaxell | rfc:parameter_type_casting_hints [2012/03/08 00:53] – Withdraw RFC ircmaxell | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2012-03-03 | * Date: 2012-03-03 | ||
* Author: Anthony Ferrara < | * Author: Anthony Ferrara < | ||
- | * Status: | + | * Status: |
* First Published at: https:// | * First Published at: https:// | ||
* Patch: https:// | * Patch: https:// | ||
Line 13: | Line 13: | ||
==== Philosophy ==== | ==== Philosophy ==== | ||
+ | This RFC discusses a method of adding scalar type hints to PHP while attempting to embrace the dynamic nature of PHP variables. | ||
- | ==== More ==== | + | It's worth noting that this RFC does not attempt to add new errors to the casting paradigm in PHP. It's my feeling that issuing errors (E_NOTICE or E_STRICT) on data-loss from cast operations is important, but a more general issue to PHP. So as such it does not fall under this specific RFC, but as a more general RFC which can be added to the language as a whole. |
- | ===== Changelog ===== | + | So in an attempt at consistency, |
+ | ===== Implementation ===== | ||
+ | ==== Syntax ==== | ||
+ | |||
+ | The parameters are type hinted using the following syntax: | ||
+ | |||
+ | <?php | ||
+ | |||
+ | function test((int) $intParam, (string) $strParam = " | ||
+ | |||
+ | ?> | ||
+ | |||
+ | This will cause the following function to always return true for almost any combination of parameters: | ||
+ | |||
+ | <?php | ||
+ | |||
+ | function test((int) $int, (float) $float, (bool) $bool, (string) $string) { | ||
+ | return is_int($int) && is_float($float) && is_bool($bool) && is_string($string); | ||
+ | } | ||
+ | |||
+ | ?> | ||
+ | |||
+ | The one exception which will cause the function to not return true, is when the '' | ||
+ | |||
+ | ==== Cast Options ==== | ||
+ | |||
+ | The following cast types are supported as parameter casts: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Note that the final 2 options (array and object) are there for both completeness, | ||
+ | |||
+ | ==== Default Values ==== | ||
+ | |||
+ | For consistency, | ||
+ | |||
+ | If '' | ||
+ | |||
+ | function test1((int) $foo) { echo gettype($foo); | ||
+ | function test2((int) $foo = null) { echo gettype($foo); | ||
+ | | ||
+ | Calling '' | ||
+ | |||
+ | ==== Backwards compatibility breaks ==== | ||
+ | |||
+ | This patch does not break existing functionality (with one exception of a C level API change that is not a public API). Current code will execute as normal. | ||
+ | |||
+ | ===== Patch Details ===== | ||
+ | |||
+ | ==== Cast Type Definitions ==== | ||
+ | |||
+ | The patch adds a series of " | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | These are used to distinguish casting declarations in OPCode from a possible future implementation of more strict casts. | ||
+ | |||
+ | These have also been added to the '' | ||
+ | ==== API Changes ==== | ||
+ | |||
+ | One API change was necessary for the cast to work. The third paramater of '' | ||
+ | |||
+ | ==== References ==== | ||
+ | |||
+ | To prevent odd behavior, it is a '' | ||
+ | |||
+ | ==== Parser Tokens ==== | ||
+ | |||
+ | No new parser tokens or reserved words are created by this RFC / implementation. | ||
+ | |||
+ | ===== Changelog ===== |
rfc/parameter_type_casting_hints.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1