Skip to content

Instantly share code, notes, and snippets.

@renegare
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save renegare/fa97f1ca0d09f34c125d to your computer and use it in GitHub Desktop.

Select an option

Save renegare/fa97f1ca0d09f34c125d to your computer and use it in GitHub Desktop.
The future of exceptions? death to the if statement?
<?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'));
@renegare

renegare commented Nov 6, 2014

Copy link
Copy Markdown
Author

credits rossillingworth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment