rfc:parameter_type_casting_hints

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:parameter_type_casting_hints [2012/03/04 01:01] – Expand RFC ircmaxellrfc: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 <ircmaxell@php.net>   * Author: Anthony Ferrara <ircmaxell@php.net>
-  * Status: In Draft+  * Status: Withdrawn
   * First Published at: https://wiki.php.net/rfc/parameter_type_casting_hints   * First Published at: https://wiki.php.net/rfc/parameter_type_casting_hints
   * Patch: https://gist.github.com/1963999   * Patch: https://gist.github.com/1963999
Line 14: Line 14:
  
 This RFC discusses a method of adding scalar type hints to PHP while attempting to embrace the dynamic nature of PHP variables.  This means that passing a type that does not match the hinted type will cause a cast to happen (if possible, based on the normal type-casting rules used in other areas of the engine).   This RFC discusses a method of adding scalar type hints to PHP while attempting to embrace the dynamic nature of PHP variables.  This means that passing a type that does not match the hinted type will cause a cast to happen (if possible, based on the normal type-casting rules used in other areas of the engine).  
 +
 +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.
 +
 +So in an attempt at consistency, this RFC uses the normal [[http://us3.php.net/manual/en/language.types.type-juggling.php|Type Juggling Rules]] that the rest of the engine uses for casting operations.
  
 ===== Implementation ===== ===== Implementation =====
Line 23: Line 27:
 <?php <?php
  
-function test((int) $intParam, (string) $strParam = "foo", (array) $array) {}+    function test((int) $intParam, (string) $strParam = "foo", (array) $array = array()) {}
  
 ?> ?>
Line 31: Line 35:
 <?php <?php
  
-function test((int) $int, (float) $float, (bool) $bool, (string) $string) { +    function test((int) $int, (float) $float, (bool) $bool, (string) $string) { 
-    return is_int($int) && is_float($float) && is_bool($bool) && is_string($string); +        return is_int($int) && is_float($float) && is_bool($bool) && is_string($string); 
-}+    }
  
 ?> ?>
Line 51: Line 55:
  
 Note that the final 2 options (array and object) are there for both completeness, and to provide a less-strict type-hint (to be consistent with the new additions). Note that the final 2 options (array and object) are there for both completeness, and to provide a less-strict type-hint (to be consistent with the new additions).
 +
 +==== Default Values ==== 
 +
 +For consistency, only matching types are allowed to be the default to a type-casted parameter.  So ''(int) $foo = null'' and ''(int) $foo = 1'' are both supported, but ''(int) $foo = "1"'' will generate an ''E_COMPILE_ERROR''.
 +
 +If ''null'' is not the default value, any attempt to pass ''null'' to a function which has a casting type hint will cause a cast from null to occur.  If ''null'' is the default value for the parameter, passing ''null'' will not trigger a cast.  So:
 +
 +    function test1((int) $foo) { echo gettype($foo); }
 +    function test2((int) $foo = null) { echo gettype($foo); }
 +    
 +Calling ''test1(null)'' will produce ''int'' as the output.  Calling ''test2(null)'' will produce ''null'' as the output.
  
 ==== Backwards compatibility breaks ==== ==== Backwards compatibility breaks ====
Line 62: Line 77:
 The patch adds a series of "cast" definitions to the C type constants currently in use by the core.  They are implemented in a way that the destination type of the cast can be found by masking the cast against 0x0F.  They are: The patch adds a series of "cast" definitions to the C type constants currently in use by the core.  They are implemented in a way that the destination type of the cast can be found by masking the cast against 0x0F.  They are:
  
-''IS_NULL_CAST 0x010'' +  * ''IS_NULL_CAST 0x010'' 
-''IS_LONG_CAST 0x011'' +  ''IS_LONG_CAST 0x011'' 
-''IS_DOUBLE_CAST 0x012'' +  ''IS_DOUBLE_CAST 0x012'' 
-''IS_BOOL_CAST 0x013'' +  ''IS_BOOL_CAST 0x013'' 
-''IS_ARRAY_CAST 0x014'' +  ''IS_ARRAY_CAST 0x014'' 
-''IS_OBJECT_CAST 0x015'' +  ''IS_OBJECT_CAST 0x015'' 
-''IS_STRING_CAST 0x016''+  ''IS_STRING_CAST 0x016''
  
 These are used to distinguish casting declarations in OPCode from a possible future implementation of more strict casts.   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 ''zend_get_type_by_const()'' function to return the cast string that would be used. These have also been added to the ''zend_get_type_by_const()'' function to return the cast string that would be used.
- 
 ==== API Changes ====  ==== API Changes ==== 
  
Line 81: Line 95:
  
 To prevent odd behavior, it is a ''E_COMPILE_ERROR'' to define a parameter as both cast-hinted and a reference.  This prevents issues where passing a variable to a function by reference changes the type of the original argument and possibly destroys data in the original variable. To prevent odd behavior, it is a ''E_COMPILE_ERROR'' to define a parameter as both cast-hinted and a reference.  This prevents issues where passing a variable to a function by reference changes the type of the original argument and possibly destroys data in the original variable.
- 
-==== Default Values ====  
- 
-For consistency, only matching types are allowed to be the default to a type-casted parameter.  So ''(int) $foo = null'' and ''(int) $foo = 1'' are both supported, but ''(int) $foo = "1"'' will generate an ''E_COMPILE_ERROR''. 
  
 ==== Parser Tokens ==== ==== Parser Tokens ====
Line 91: Line 101:
  
 ===== Changelog ===== ===== Changelog =====
- 
rfc/parameter_type_casting_hints.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1