rfc:throwable-interface
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:throwable-interface [2015/05/22 18:00] – trowski | rfc:throwable-interface [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Throwable Interface ====== | ====== PHP RFC: Throwable Interface ====== | ||
- | * Version: 0.1.3 | + | * Version: 0.1.4 |
* Date: 2015-05-22 | * Date: 2015-05-22 | ||
* Author: Aaron Piotrowski < | * Author: Aaron Piotrowski < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | PHP 7 has introduced exceptions as a replacement for fatal or recoverable fatal errors. These exceptions do not extend '' | + | PHP 7 has introduced exceptions as a replacement for fatal or recoverable fatal errors |
+ | |||
+ | **Since the decision was made to separate the exception branches, then that separation should be clear instead of obfuscated by similar class names.** | ||
Example: | Example: | ||
+ | |||
<code php> | <code php> | ||
function add(int $left, int $right) { | function add(int $left, int $right) { | ||
return $left + $right; | return $left + $right; | ||
- | }; | + | } |
try { | try { | ||
Line 28: | Line 31: | ||
</ | </ | ||
- | The reason an object named '' | + | The reason an object named '' |
+ | |||
+ | To catch the '' | ||
+ | |||
+ | <code php> | ||
+ | function add(int $left, int $right) { | ||
+ | return $left + $right; | ||
+ | } | ||
+ | |||
+ | try { | ||
+ | echo add(' | ||
+ | } catch (Exception $e) { | ||
+ | // Handle exception | ||
+ | } catch (TypeException $e) { // Appears to descend from Exception | ||
+ | // Log error and end gracefully | ||
+ | } | ||
+ | </ | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 41: | Line 60: | ||
'' | '' | ||
- | Only objects that implement the Throwable interface can be thrown. The proposed patch does not allow userland classes to implement the Throwable interface. Instead all classes declared in userland must extend one of the existing exception classes. | + | Note that '' |
+ | |||
+ | Only objects that implement the '' | ||
The '' | The '' | ||
Line 54: | Line 75: | ||
While both '' | While both '' | ||
- | '' | + | '' |
+ | |||
+ | After this proposed change, it would be clearer in the example above that another catch block is needed if the user wishes to log errors and end the script gracefully. | ||
+ | |||
+ | <code php> | ||
+ | function add(int $left, int $right) { | ||
+ | return $left + $right; | ||
+ | } | ||
+ | |||
+ | try { | ||
+ | echo add(' | ||
+ | } catch (Exception $e) { | ||
+ | // Handle exception | ||
+ | } catch (Error $e) { // Clearly a different type of object | ||
+ | // Log error and end gracefully | ||
+ | } | ||
+ | </ | ||
=== Error Name Choice === | === Error Name Choice === | ||
- | The name Error was chosen to correspond with PHP's other errors. Non-fatal errors detected by PHP will continue to trigger warnings and notices, while fatal errors are thrown as Error exceptions. | + | The name '' |
Conceptually both of these conditions are error conditions detected by PHP. The only difference is that for some errors the execution of the script can continue from where the error occurred; for others it is not possible for execution to continue from the place where the error occurred, and so instead an exception must be thrown. | Conceptually both of these conditions are error conditions detected by PHP. The only difference is that for some errors the execution of the script can continue from where the error occurred; for others it is not possible for execution to continue from the place where the error occurred, and so instead an exception must be thrown. | ||
- | Users may wish to use set_error_handler() to throw objects extending Error in their code for non-fatal types of error. | + | While this name may also cause some confusion for users, other name choices such as '' |
- | + | ||
- | While this name may also cause some confusion for users, other name choices such as Failure do not seem appropriate. It is likely that users would use the term ' | + | |
=== AssertionException === | === AssertionException === | ||
Line 74: | Line 110: | ||
===== Backwards Compatibility ===== | ===== Backwards Compatibility ===== | ||
- | '' | + | '' |
===== Patch ===== | ===== Patch ===== | ||
A patch for this RFC is available at [[https:// | A patch for this RFC is available at [[https:// | ||
+ | |||
+ | ===== Voting ===== | ||
+ | A majority of 50%+1 is required to approve this RFC. | ||
+ | |||
+ | Please remember that this vote is not about creating separate exception branches, as that decision was made in the [[https:// | ||
+ | |||
+ | Voting opened June 10th, 2015 and remained open until June 17th, 2015. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== References ===== | ===== References ===== |
rfc/throwable-interface.1432317631.txt.gz · Last modified: 2017/09/22 13:28 (external edit)