rfc:returntypehint2

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:returntypehint2 [2011/12/21 22:21] – [Syntactical Implementation] fitchwhrfc:returntypehint2 [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2011-12-13   * Date: 2011-12-13
   * Author: Will Fitch <will.fitch@gmail.com>   * Author: Will Fitch <will.fitch@gmail.com>
-  * Status: Under Discussion+  * Status: Withdrawn
  
  
Line 27: Line 27:
  
 This implementation of return type hinting follows the same approach.  This provides consistency that developers appreciate.  When and if type hinting allows for additional types, the change can be applied to both methods and parameters. This implementation of return type hinting follows the same approach.  This provides consistency that developers appreciate.  When and if type hinting allows for additional types, the change can be applied to both methods and parameters.
 +
 +=== Returning NULL ===
 +
 +By default, if you specify a return type hint, you **must** return that type.  However, a new keyword "nullable" has been added to bypass this requirement.  Much like the parameter type hinting, this provides developers with added flexibility, and allows developers using an API to easily identify and code for that situation.
  
 === Example Implementations === === Example Implementations ===
Line 56: Line 60:
     }     }
          
-    public array getNullValue()+    public callable getCallableString() 
 +    { 
 +        return 'strlen'; 
 +    } 
 +     
 +    // The nullable keyword allows you to return null  
 +    protected nullable ArrayObject getArrayObject()
     {     {
         return null;         return null;
     }     }
          
-    public callable getCallableString()+    private callable methodModsDontMatter()
     {     {
-        return 'strlen';+        return 'str_replace'
 +    } 
 +     
 +    ArrayObject methodsDontNeedModsActually() 
 +    { 
 +        return new ArrayObject();
     }     }
 } }
Line 128: Line 143:
 === Parser === === Parser ===
  
-The language parser "Zend/zend_language_parser.y" has been modified to add an additional term called "method_return_type" The definition for this contains one token, T_ARRAY, and one term - fully_qualified_class_name.  The method_return_type was added to class_statement, just above "function".  +The language parser "Zend/zend_language_parser.y" has been modified to add an additional term called "method_return_type" The definition for this contains two tokens, T_CALLABLE, T_ARRAY, and one term - fully_qualified_class_name.  The method_return_type was added to class_statement, just above "function".  
  
 I separated the "function" and "method_return_type" definitions within class_statement for two reasons.  First, I didn't want to introduce any additional changes to the compiler function zend_do_begin_function_declaration.  Since all function and method declarations go through this today, I didn't want to add logic within this just to verify if a method has defined a type hint.  This would've ended up in multiple places throughout the function, so I thought it would be best to let the parser deal with this once.  I separated the "function" and "method_return_type" definitions within class_statement for two reasons.  First, I didn't want to introduce any additional changes to the compiler function zend_do_begin_function_declaration.  Since all function and method declarations go through this today, I didn't want to add logic within this just to verify if a method has defined a type hint.  This would've ended up in multiple places throughout the function, so I thought it would be best to let the parser deal with this once. 
Line 154: Line 169:
 === Tests === === Tests ===
  
-A total of 20 tests were added to tests/classes/ All tests file names are prefixed with "method_returntype_" for easy identification.  The tests do the following:+A total of 21 tests were added to tests/classes/ All tests file names are prefixed with "method_returntype_" for easy identification.  The tests do the following:
  
   * Determine that non-namespaced and namespaced class name don't produce a syntax error   * Determine that non-namespaced and namespaced class name don't produce a syntax error
   * Determines if a catchable fatal error is raised when an array or object is defined and the following are returned: resource, object, string, integer or double.  There are referenced return counterparts for these as well.   * Determines if a catchable fatal error is raised when an array or object is defined and the following are returned: resource, object, string, integer or double.  There are referenced return counterparts for these as well.
   * Determine if an E_COMPILE_ERROR is raised when a class implements an interface that defines a method which returns both an array and object, but fails to correctly redefine.    * Determine if an E_COMPILE_ERROR is raised when a class implements an interface that defines a method which returns both an array and object, but fails to correctly redefine. 
 +  * Determines if a callable works
 +  * Determines if a callable is defined but not returned
  
  
 ===== Patch ===== ===== Patch =====
  
-The downloadable patch below can be applied against PHP HEADAlternatively, I keep clone a of PHP in my own GitHub sandbox.  I have provided the link to it below.  Please take note of the branch name to see the right code. +The patch for this is now outdated and gone
- +
-Patch: http://www.willfitch.com/php/returntype.patch +
- +
-Github: https://github.com/fitchwh/php-src (returntype branch)+
  
  
Line 174: Line 187:
 ===== Changelog ===== ===== Changelog =====
  
 +  - Updated to include "callable" as an accepted return type. Includes patch and documentation changes 
 +  - Updated to remove allowing NULL to be returned unconditionally when declaring return types 
 +  - Added a new patch which includes a "nullable" token for declaring a method may return null 
 +  - Updated the RFC to take "nullable" into account and replaced the old patch with the new functionality
  
rfc/returntypehint2.1324506110.txt.gz · Last modified: 2017/09/22 13:28 (external edit)