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
rfc:parameter_type_casting_hints [2012/03/04 02:48] – Update philosophy section to indicate why it's normal casting only ircmaxellrfc:parameter_type_casting_hints [2017/09/22 13:28] (current) – external edit 127.0.0.1
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 27: Line 27:
 <?php <?php
  
-    function test((int) $intParam, (string) $strParam = "foo", (array) $array) {}+    function test((int) $intParam, (string) $strParam = "foo", (array) $array = array()) {}
  
 ?> ?>
Line 55: 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 84: 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 94: Line 101:
  
 ===== Changelog ===== ===== Changelog =====
- 
rfc/parameter_type_casting_hints.1330829296.txt.gz · Last modified: 2017/09/22 13:28 (external edit)