Last active
August 29, 2015 14:08
-
-
Save renegare/fa97f1ca0d09f34c125d to your computer and use it in GitHub Desktop.
The future of exceptions? death to the if statement?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Will; | |
/** | |
* This class replaces if statement that throws exceptions with nice | |
* readable (DSL) code | |
*/ | |
class Throw { | |
/** | |
* @param bool $condition | |
* @param Exception $exception | |
* @throws Exception - throws $exception when $condition is true | |
*/ | |
public static function when($condition, \Exception $exception) { | |
if($condition) { | |
throw $exception; | |
} | |
} | |
} | |
# example usage: | |
\Will\Throw::when(true != false, new LogicException('blah blah')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
credits rossillingworth