rfc:safe_cast
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:safe_cast [2014/11/14 20:14] – v0.1.5 - to_ and try_ ajf | rfc:safe_cast [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Safe Casting Functions ====== | ====== PHP RFC: Safe Casting Functions ====== | ||
- | * Version: 0.1.5 | + | * Version: 0.1.8 |
- | * Date: 2014-10-20, Last Updated 2014-11-12 | + | * Date: 2014-10-20, Last Updated 2014-11-14 |
* Author: Andrea Faulds, ajf@ajf.me | * Author: Andrea Faulds, ajf@ajf.me | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 9: | Line 9: | ||
Currently, PHP only provides one means of type conversion: explicit casts. These casts never fail or emit errors, making them dangerous to use, as when passed garbage input, they will simply return garbage instead of indicating that something went wrong. This makes it difficult to write robust applications which handle user data. They also prevent any suggestion of strict type hinting for scalar types, because if that were to be added, users would simply use dangerous explicit casts to get around errors and the result would be code that is buggier than it would have been without type hinting at all. | Currently, PHP only provides one means of type conversion: explicit casts. These casts never fail or emit errors, making them dangerous to use, as when passed garbage input, they will simply return garbage instead of indicating that something went wrong. This makes it difficult to write robust applications which handle user data. They also prevent any suggestion of strict type hinting for scalar types, because if that were to be added, users would simply use dangerous explicit casts to get around errors and the result would be code that is buggier than it would have been without type hinting at all. | ||
+ | |||
+ | For int and float conversion specifically, | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 14: | Line 16: | ||
Two families of "safe casting" | Two families of "safe casting" | ||
- | '' | + | '' |
- | '' | + | '' |
'' | '' | ||
Line 26: | Line 28: | ||
The concept was developed in my thoughts, and in discussions with Anthony Ferrara (both in-person at PHPNW14 and online) and others in [[http:// | The concept was developed in my thoughts, and in discussions with Anthony Ferrara (both in-person at PHPNW14 and online) and others in [[http:// | ||
- | * The functions | + | * The functions |
- | * Floats | + | * Whitespace is not allowed because it is expected that most input will lack it, and it can be easily stripped before conversion using '' |
- | * Objects with '' | + | * Generally, no data is allowed to be lost, with the exception of floats, which are allowed to lose accuracy from their decimal representations. This is because zero data-loss is impossible |
- | * '' | + | * Leading zeros are not accepted for '' |
- | * '' | + | |
* There are two sets of functions, one that returns NULL on failure and the other that throws an exception for the following reasons: | * There are two sets of functions, one that returns NULL on failure and the other that throws an exception for the following reasons: | ||
* In some cases, invalid input is an exceptional case, so an exception is desirable, but in other cases, it is not exceptional, | * In some cases, invalid input is an exceptional case, so an exception is desirable, but in other cases, it is not exceptional, | ||
- | * It is often repeated that exceptions shouldn' | + | * It is often repeated that exceptions shouldn' |
- | * It is PHP tradition to allow both object-oriented (exceptions) and procedural (error return value) approaches to problems | + | * It is PHP tradition to allow both object-oriented (exceptions) and procedural (error return value) approaches to problems. |
- | * This placates both people who would like an error return value, and people who would like exceptions | + | * This placates both people who would like an error return value, and people who would like exceptions. |
- | * Return values are better for chaining in some circumstances | + | * Return values are better for chaining in some circumstances. |
- | * Checking exceptions is generally slower than checking return values | + | * Checking exceptions is generally slower than checking return values, especially in HHVM). |
==== Examples Table ==== | ==== Examples Table ==== | ||
Line 51: | Line 52: | ||
^ string(2) " | ^ string(2) " | ||
^ string(3) " | ^ string(3) " | ||
- | ^ string(3) " | + | ^ string(3) " |
^ string(3) " | ^ string(3) " | ||
^ int(10) | ^ int(10) | ||
Line 93: | Line 94: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | While I'd prefer to return NULL on error, it would also be possible to return FALSE. As this seems to be relatively controversial, | + | None. |
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
Line 106: | Line 107: | ||
As this is not a language change and only introduces new functions, only a 50%+1 majority will be required. The vote will be a straight Yes/No vote on accepting the RFC and merging the patch into master. | As this is not a language change and only introduces new functions, only a 50%+1 majority will be required. The vote will be a straight Yes/No vote on accepting the RFC and merging the patch into master. | ||
+ | |||
+ | ==== Vote ==== | ||
+ | |||
+ | Voting opened 2014-11-19 and ended 2014-11-29. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
Line 131: | Line 141: | ||
===== Changelog ===== | ===== Changelog ===== | ||
+ | * v0.1.8 - ext/filter note in Introduction | ||
+ | * v0.1.7 - Allow positive signs | ||
+ | * v0.1.6 - Dropped zero round trip data loss principle, added octal and whitespace rationale | ||
* v0.1.5 - Renamed '' | * v0.1.5 - Renamed '' | ||
* v0.1.4 - Reject leading ' | * v0.1.4 - Reject leading ' |
rfc/safe_cast.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1