rfc:typecheckingstrictandweak

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
Next revisionBoth sides next revision
rfc:typecheckingstrictandweak [2010/05/27 18:54] – added note about stricter auto conversion rules lsmithrfc:typecheckingstrictandweak [2010/06/09 14:18] zeev
Line 27: Line 27:
  
 The proposed solution implements a 'weaker' kind of type hinting - which arguably is more consistent with the rest of PHP's type system. The proposed solution implements a 'weaker' kind of type hinting - which arguably is more consistent with the rest of PHP's type system.
-Instead of validating the zval.type property only - it uses similar rules to those used by PHP's auto-conversion system to look into the value in question, and determine whether it 'makes sense' in the required context.  If it does - it will be converted to the required type (if it isn't already of that type);  If it doesn't - an error will be generated.+Instead of validating the zval.type property only - it uses rules in line with the spirit of PHP and it's auto-conversion system to look into the value in question, and determine whether it 'makes sense' in the required context.  If it does - it will be converted to the required type (if it isn't already of that type);  If it doesn't - an error will be generated.
  
-For example, consider a function getUserById() that expects an integer value.  With [[http://news.php.net/php.internals/44573|strict type hinting]], if you feed it with $id, which happens to hold a piece of data from the database with the string value "42", it will be rejected.  With auto-converting type hinting, PHP will determine that $id is a string that has an integer format - and it is therefore suitable to be fed into getUserById().  It will then convert the value it to an integer, and pass it on to getUserById().  That means that getUserById() can rely that it will *always* get its input as an integer - but the caller will still have the luxury of sending non-integer but integer-formatted input to it.  Also note that the conversion rules proposed here are slightly stricter than PHP's auto-conversion rules.  Mainly, the string "abc" will be rejected as valid input for an integer type-hinted argument, and not be passed-on as zero.+For example, consider a function getUserById() that expects an integer value.  With [[http://news.php.net/php.internals/44573|strict type hinting]], if you feed it with $id, which happens to hold a piece of data from the database with the string value "42", it will be rejected.  With auto-converting type hinting, PHP will determine that $id is a string that has an integer format - and it is therefore suitable to be fed into getUserById().  It will then convert the value it to an integer, and pass it on to getUserById().  That means that getUserById() can rely that it will **always** get its input as an integer - but the caller will still have the luxury of sending non-integer but integer-formatted input to it.
  
-However the here proposed auto-conversion would be a bit stricter than the now standard rules. For example it would not auto-convert from/to array's. Furthermore it would either raise a notice or error out (TBD) if due to auto-conversion there would be data loss. So for example "2", 2 as well as 2.5 would convert to a float if one is expected. However 2.5 would not silently convert to an integer if one is expectedSimilarly "123 abc" would not convert to an integer or float. This might also be a potential approach to type juggling in general in some future version of PHP+The key advantages of the proposed solutions are that there's less burden on those calling APIs (fail only when really necessary). It should be noted that most of the time coding is spend consuming existing API'and not creating new ones. Furthermore it's consistent with the rest of PHP in the sense that most of PHP does not care about exact matching zval types, and perhaps most importantly it does not require everyone to become intimately familiar with PHP's type system.
  
-The key advantages of the proposed solution are that there's less burden on those calling APIs (fail only when really necessary). It should be noted that most of the time coding is spend consuming existing API's and not creating new onesFurthermore it'consistent with the rest of PHP in the sense that most of PHP does not care about exact matching zval types, and perhaps most importantly - it does not require everyone to become intimately familiar with PHP's type system.+Furthermore, weak type hinting may be a step on the way to create generic type casting magic methods along the lines of %%__toString()%%, allowing objects to auto-convert to scalar types as necessary (TBD). 
 + 
 +===== Option (1): current type juggeling rules with E_STRICT on data loss ===== 
 + 
 +The auto-conversion would follow the current type juggeling rules. However in case of a cast that leads to data loss (like casting from '123abc' to an integer leading to 123 an E_STRICT notice would be raised. 
 + 
 +For reference, here's the current behavior of zend_parse_parameters, used in most internal functions. 
 + 
 +^ value                     ^ string ^ float  ^ int    ^ bool   ^ array  ^ 
 +^ true (boolean)            | pass   | pass   | pass   | pass   | fail   | 
 +^ false (boolean)           | pass   | pass   | pass   | pass   | fail   | 
 +^ 0 (integer)               | pass   | pass   | pass   | pass   | fail   | 
 +^ 1 (integer)               | pass   | pass   | pass   | pass   | fail   | 
 +^ 12 (integer)              | pass   | pass   | pass   | pass   | fail   | 
 +^ 12 (double)               | pass   | pass   | pass   | pass   | fail   | 
 +^ 12.34 (double)            | pass   | pass   | pass   | pass   | fail   | 
 +^ 'true' (string)           | pass   | fail   | fail   | pass   | fail   | 
 +^ 'false' (string)          | pass   | fail   | fail   | pass   | fail   | 
 +^ '0' (string)              | pass   | pass   | pass   | pass   | fail   | 
 +^ '1' (string)              | pass   | pass   | pass   | pass   | fail   | 
 +^ '12' (string)             | pass   | pass   | pass   | pass   | fail   | 
 +^ '12abc' (string)          | pass   | pass   | pass   | pass   | fail   | 
 +^ '12.0' (string)           | pass   | pass   | pass   | pass   | fail   | 
 +^ '12.34' (string)          | pass   | pass   | pass   | pass   | fail   | 
 +^ 'foo' (string)            | pass   | fail   | fail   | pass   | fail   | 
 +^ array   (array)           | fail   | fail   | fail   | fail   | pass   | 
 +^ array(0=>12) (array)      | fail   | fail   | fail   | fail   | pass   | 
 +^ NULL (NULL)               | pass   | pass   | pass   | pass   | fail   | 
 +^ %%''%% (string)               | pass   | fail   | fail   | pass   | fail   | 
 +===== Option (2): new type juggeling rules with E_STRICT on data loss ===== 
 + 
 +The conversion rules proposed here are slightly stricter than PHP's auto-conversion rules.  Mainly, the string "abc" will be rejected as valid input for an integer type-hinted argument, and not be passed-on as zero and it would not auto-convert from/to array's
 + 
 +An E_STRICT would be raised if due to auto-conversion there would be data loss. So for example "2", 2 as well as 2.5 would convert to a float if one is expected. However 2.5 would not silently convert to an integer if one is expected. Similarly "123abc" would not convert to an integer or float. This might also be a potential approach to type juggling in general in some future version of PHP.
  
 Here is a short list of examples to illustrate the weak type hinting. Note that just like the current array/object hints, a NULL is only allowed if the parameter defaults to NULL. Here is a short list of examples to illustrate the weak type hinting. Note that just like the current array/object hints, a NULL is only allowed if the parameter defaults to NULL.
Line 60: Line 93:
 ^ %%''%% (string)         | pass   | fail  | fail  | fail    | pass   | fail | fail  | ^ %%''%% (string)         | pass   | fail  | fail  | fail    | pass   | fail | fail  |
  
-Furthermore, weak type hinting may be a step on the way to create generic type casting magic methods along the lines of %%__toString()%%, allowing objects to auto-convert to scalar types as necessary (TBD).+===== Option (3): current type juggeling rules with E_FATAL on data loss =====
  
-===== Current behavior of zend_parse_parameters =====+The auto-conversion would follow the current type juggeling rules. However in case of a cast that leads to data loss (like casting from '123abc' to an integer leading to 123 an E_FATAL notice would be raised. 
 +===== Patch =====
  
-For reference, here's the current behavior of zend_parse_parameters, used in most internal functions. +  * {{:rfc:auto_converting_type_hinting.diff.txt}} - presently implements auto-converting type hinting without any warning on data loss.
- +
-^ value                     ^ string ^ float  ^ int    ^ bool   ^ array  ^ +
-^ true (boolean)            | pass   | pass   | pass   | pass   | fail   | +
-^ false (boolean)           | pass   | pass   | pass   | pass   | fail   | +
-^ 0 (integer)               | pass   | pass   | pass   | pass   | fail   | +
-^ 1 (integer)               | pass   | pass   | pass   | pass   | fail   | +
-^ 12 (integer)              | pass   | pass   | pass   | pass   | fail   | +
-^ 12 (double)               | pass   | pass   | pass   | pass   | fail   | +
-^ 12.34 (double)            | pass   | pass   | pass   | pass   | fail   | +
-^ 'true' (string)           | pass   | fail   | fail   | pass   | fail   | +
-^ 'false' (string)          | pass   | fail   | fail   | pass   | fail   | +
-^ '0' (string)              | pass   | pass   | pass   | pass   | fail   | +
-^ '1' (string)              | pass   | pass   | pass   | pass   | fail   | +
-^ '12' (string)             | pass   | pass   | pass   | pass   | fail   | +
-^ '12abc' (string)          | pass   | pass   | pass   | pass   | fail   | +
-^ '12.0' (string)           | pass   | pass   | pass   | pass   | fail   | +
-^ '12.34' (string)          | pass   | pass   | pass   | pass   | fail   | +
-^ 'foo' (string)            | pass   | fail   | fail   | pass   | fail   | +
-^ array   (array)           | fail   | fail   | fail   | fail   | pass   | +
-^ array(0=>12) (array)      | fail   | fail   | fail   | fail   | pass   | +
-^ NULL (NULL)               | pass   | pass   | pass   | pass   | fail   | +
-^ %%''%% (string)               | pass   | fail   | fail   | pass   | fail   |+
  
 ===== Changelog ===== ===== Changelog =====
 +  * restructured to provide 3 options (two with current type juggeling rules and E_STRICT or E_FATAL on data loss conversion and one with new type juggeling rules and E_STRICT on data loss.
rfc/typecheckingstrictandweak.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1