rfc:scalar_type_hints_v5

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:scalar_type_hints_v5 [2015/02/19 20:12]
ircmaxell Add part about narrowing conversion.
rfc:scalar_type_hints_v5 [2018/10/17 11:38]
carusogabriel Fix table align in "bool" column
Line 1: Line 1:
 ====== PHP RFC: Scalar Type Declarations ====== ====== PHP RFC: Scalar Type Declarations ======
-  * Version: 0.5.1+  * Version: 0.5.3
   * Date: 2015-02-18   * Date: 2015-02-18
   * Author: Anthony Ferrara <ircmaxell@php.net> (original Andrea Faulds, ajf@ajf.me)   * Author: Anthony Ferrara <ircmaxell@php.net> (original Andrea Faulds, ajf@ajf.me)
-  * Status: Discussion+  * Status: Implemented
   * First Published at: http://wiki.php.net/rfc/scalar_type_hints_v5   * First Published at: http://wiki.php.net/rfc/scalar_type_hints_v5
   * Forked From: http://wiki.php.net/rfc/scalar_type_hints   * Forked From: http://wiki.php.net/rfc/scalar_type_hints
Line 125: Line 125:
 The table shows which types are accepted and converted for scalar type declarations. ''NULL'', arrays and resources are never accepted for scalar type declarations, and so are not included in the table. The table shows which types are accepted and converted for scalar type declarations. ''NULL'', arrays and resources are never accepted for scalar type declarations, and so are not included in the table.
  
-^ Type declaration integer  ^ float    ^ string   boolean ^ object +^ Type declaration int  ^ float    ^ string   bool  ^ object 
-^ ''integer''       ^ yes      ^ yes*     ^ yes†     ^ yes     ^ no      ^ +^ ''int''           ^ yes  ^ yes*     ^ yes†     ^ yes   ^ no      ^ 
-^ ''float''         ^ yes      ^ yes      ^ yes†     ^ yes     ^ no      ^ +^ ''float''         ^ yes  ^ yes      ^ yes†     ^ yes   ^ no      ^ 
-^ ''string''        ^ yes      ^ yes      ^ yes      ^ yes     ^ yes‡    ^ +^ ''string''        ^ yes  ^ yes      ^ yes      ^ yes   ^ yes‡    ^ 
-^ ''boolean''       ^ yes      ^ yes      ^ yes      ^ yes     ^ no      ^+^ ''bool''          ^ yes  ^ yes      ^ yes      ^ yes   ^ no      ^
  
 <nowiki>*</nowiki>Only non-NaN floats between ''PHP_INT_MIN'' and ''PHP_INT_MAX'' accepted. (New in PHP 7, see the [[rfc:zpp_fail_on_overflow|ZPP Failure on Overflow]] RFC) <nowiki>*</nowiki>Only non-NaN floats between ''PHP_INT_MIN'' and ''PHP_INT_MAX'' accepted. (New in PHP 7, see the [[rfc:zpp_fail_on_overflow|ZPP Failure on Overflow]] RFC)
Line 145: Line 145:
 These strict type checking rules are used for userland scalar type declarations, and for extension and built-in PHP functions. These strict type checking rules are used for userland scalar type declarations, and for extension and built-in PHP functions.
  
-The one exception is that [[http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2|widening primitive conversion]] is allowed. This means that parameters that declare ''float'' can also accept ''int''.+The one exception is that [[http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2|widening primitive conversion]] is allowed for ''int'' to ''float''. This means that parameters that declare ''float'' can also accept ''int''.
  
 <file php widening.php> <file php widening.php>
Line 159: Line 159:
  
 In this case, we're passing an ''int'' to a function that accepts ''float''. The parameter is converted (widened) to float. In this case, we're passing an ''int'' to a function that accepts ''float''. The parameter is converted (widened) to float.
 +
 +No other conversions are allowed.
 +
 +==== Error Handler Behavior In Strict Mode ====
 +
 +Currently it's possible to bypass error check failures using an error handler:
 +
 +<file php error_handler_fail.php>
 +<?php
 +declare(strict_types=1);
 +set_error_handler(function() {
 +    return true;
 +});
 +
 +function foo(int $abc) {
 +    var_dump($abc);
 +}
 +foo("test"); // string(4) "test"
 +?>
 +</file>
 +
 +This would defeat the purpose of strict typing. 
 +
 +Therefore, this RFC proposes to bypass function execution in strict mode if there's a type mismatch error (just like internal functions do today). The implementation is not complete, as this behavior would be superseded by [[rfc:engine_exceptions]] if it passed. Therefore the implementation will wait for the completion of voting on that RFC.
 +
  
 ===== Example ===== ===== Example =====
Line 566: Line 591:
 Allowing strict "blocks" can create situations where a single file uses several "type modes". This can hamper readability and make working on typed code significantly harder. Allowing strict "blocks" can create situations where a single file uses several "type modes". This can hamper readability and make working on typed code significantly harder.
  
-Therefore, this proposal explicitly disallows changing the type mode anywhere within the file except the first line. Since the first line is the only allowed type change, block mode does not make sense (as there could only ever be a single block in the file). +Therefore, this proposal explicitly disallows changing the type mode anywhere within the file except the first line. Since the first line is the only allowed type change, block mode does not make sense (as there could only ever be a single block in the file)
 + 
 +Additionally, some technical limitations do make it significantly more difficult: [[http://news.php.net/php.internals/83356|Email describing limitiations]].
  
 ==== Internal Functions Do Not Have Declared Return Types ==== ==== Internal Functions Do Not Have Declared Return Types ====
Line 613: Line 640:
  
 === Current Position === === Current Position ===
 +https://wiki.php.net/rfc/reserve_more_types_in_php_7
 This is not a two-part proposal. The proposal is of a unified system that was designed to work together. As such, neither part (weak-only or strict-only) is designed to stand on its own without the other part. This is not a two-part proposal. The proposal is of a unified system that was designed to work together. As such, neither part (weak-only or strict-only) is designed to stand on its own without the other part.
  
Line 653: Line 680:
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
  
-This is proposed for the next PHP x, currently PHP 7. The acceptance for said version will depend on timing of the vote completion.+This proposal targets the 7.0 release of PHP.
  
 ===== RFC Impact ===== ===== RFC Impact =====
Line 676: Line 703:
  
 As this is a language change, this RFC requires a 2/3 majority to pass. As this is a language change, this RFC requires a 2/3 majority to pass.
 +
 +<doodle title="Accept Scalar Type Declarations With Optional Strict Mode?" auth="ircmaxell" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
 +
 +This vote is opened on February 26th, 2015 and will close March 16th at 21:00 UTC as announced on list.
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 697: Line 731:
 ===== Changelog ===== ===== Changelog =====
  
 +  * v0.5.3 Change version target back and add line about bypassing function execution on type error in strict mode
 +  * v0.5.2 Change version target
   * v0.5.1 Remove aliases from proposal   * v0.5.1 Remove aliases from proposal
   * v0.5 Fork from Andrea's original proposal. Change declare behavior. Add int->float (primitive type widening).   * v0.5 Fork from Andrea's original proposal. Change declare behavior. Add int->float (primitive type widening).
rfc/scalar_type_hints_v5.txt · Last modified: 2018/10/17 11:38 by carusogabriel