rfc:return_types

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:return_types [2015/01/14 00:38] – Grammar levimrfc:return_types [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 4: Line 4:
   * Date: 2014-03-20    * Date: 2014-03-20 
   * Author: Levi Morrison <levim@php.net>   * Author: Levi Morrison <levim@php.net>
-  * Status: Under Discussion+  * Status: Implemented (PHP 7.0)
   * First Published at: https://wiki.php.net/rfc/returntypehinting   * First Published at: https://wiki.php.net/rfc/returntypehinting
   * Migrated to: https://wiki.php.net/rfc/return_types   * Migrated to: https://wiki.php.net/rfc/return_types
Line 13: Line 13:
 Declaring return types has several motivators and use-cases: Declaring return types has several motivators and use-cases:
   * Prevent sub-types from breaking the expected return type of the super-type((See [[#variance_and_signature_validation|Variance and Signature Validation]] and [[#examples]] for more details on how this works)), especially in interfaces   * Prevent sub-types from breaking the expected return type of the super-type((See [[#variance_and_signature_validation|Variance and Signature Validation]] and [[#examples]] for more details on how this works)), especially in interfaces
-  * Prevent unintended return types+  * Prevent unintended return values
   * Document return type information in a way that is not easily invalidated (unlike comments)   * Document return type information in a way that is not easily invalidated (unlike comments)
  
 ===== Proposal ===== ===== Proposal =====
-This proposal adds an optional return type declaration to function declarations including closures, functions, generators, interface method declarations and class declarations. This RFC does not change the existing type declarations nor does it add new ones (see [[#differences_from_past_rfcs|differences from past RFCs]]).+This proposal adds an optional return type declaration to function declarations including closures, functions, generators,  and methods. This RFC does not change the existing type declarations nor does it add new ones (see [[#differences_from_past_rfcs|differences from past RFCs]]).
  
 Here is a brief example of the syntax in action: Here is a brief example of the syntax in action:
Line 166: Line 166:
 get_config(); get_config();
 </PHP> </PHP>
-''Catchable fatal error: The function get_config was expected to return an array and returned an integer in %s on line %d''+''Catchable fatal error: Return value of get_config() must be of the type arrayinteger returned in %s on line %d''
  
 ---- ----
Line 178: Line 178:
 answer(); answer();
 </PHP> </PHP>
-''Catchable fatal error: The function answer was expected to return an object of class int and returned an integer in %s on line %d''+''Catchable fatal error: Return value of answer() must be an instance of intinteger returned in %s on line %d''
  
 ---- ----
Line 190: Line 190:
 foo(); foo();
 </PHP> </PHP>
-''Catchable fatal error: The function foo was expected to return an object of class DateTime and returned null in %s on line %d''+''Catchable fatal error: Return value of foo() must be an instance of DateTimenull returned in %s on line %d''
  
 ---- ----
Line 210: Line 210:
 } }
 </PHP> </PHP>
-''Fatal error: Declaration of UserGateway_MySql::find should be compatible with UserGateway::find($id): User, return type missing in %s on line %d''+''Fatal error: Declaration of UserGateway_MySql::find() must be compatible with UserGateway::find($id): User in %s on line %d''
  
 ---- ----
Line 227: Line 227:
 This proposal specifically does not allow declaring multiple return types; this is out of the scope of this RFC and would require a separate RFC if desired. This proposal specifically does not allow declaring multiple return types; this is out of the scope of this RFC and would require a separate RFC if desired.
  
-If you want to use multiple return types, simply omit a return type declaration and rely on PHP's excellent dynamic nature.+If you want to use multiple return types in the meantime, simply omit a return type declaration and rely on PHP's excellent dynamic nature.
  
 ==== Reflection ==== ==== Reflection ====
Line 262: Line 262:
 This RFC modifies the PHP language syntax and therefore requires a two-third majority of votes. This RFC modifies the PHP language syntax and therefore requires a two-third majority of votes.
  
-<del>Should return types as outlined in this RFC be added to the PHP language? Voting will end on November 14th2014.</del> +Should return types as outlined in this RFC be added to the PHP language? Voting will end on January 232015
-A bug was found during the voting period that will require enough changes to how the RFC works that voting has been cancelled+<doodle title="Typed Returns" auth="levim" voteType="single" closed="true">
-<doodle title="Return Types" auth="levim" voteType="single" closed="true">+
    * Yes    * Yes
    * No    * No
Line 272: Line 271:
  
 Dmitry and I have updated the implementation to a more current master branch here: https://github.com/php/php-src/pull/997 Dmitry and I have updated the implementation to a more current master branch here: https://github.com/php/php-src/pull/997
 +
 +This RFC was merged into the master branch (PHP 7) in commit [[https://git.php.net/?p=php-src.git;a=commit;h=638d0cb7531525201e00577d5a77f1da3f84811e|638d0cb7531525201e00577d5a77f1da3f84811e]].
  
 ===== Future Work ===== ===== Future Work =====
rfc/return_types.1421195939.txt.gz · Last modified: 2017/09/22 13:28 (external edit)