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 revision Previous revision
Next revision
Previous revision
rfc:parameter_type_casting_hints [2012/03/04 01:27]
ircmaxell [Cast Type Definitions]
rfc:parameter_type_casting_hints [2017/09/22 13:28] (current)
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 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 80: 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 90: Line 101:
  
 ===== Changelog ===== ===== Changelog =====
- 
rfc/parameter_type_casting_hints.1330824453.txt.gz · Last modified: 2017/09/22 13:28 (external edit)