Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nicklasring
Created April 1, 2018 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicklasring/5d424f7308c185dea65c27d6bc9f7e5b to your computer and use it in GitHub Desktop.
Save nicklasring/5d424f7308c185dea65c27d6bc9f7e5b to your computer and use it in GitHub Desktop.
<?php
class TestExceptionHandler extends Exception {
public static function external_logging($callable) {
set_exception_handler(array(
__CLASS__, $callable
));
}
// TODO: PHP7 (Throwable $e)
public static function api(Exception $e)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://localhost:8080/get?blabla'
));
$result = curl_exec($curl);
curl_close($curl);
throw $e;
}
}
class TestException
{
public $var;
const THROW_NONE = 0;
const THROW_CUSTOM = 1;
const THROW_DEFAULT = 2;
function __construct($avalue = self::THROW_NONE) {
TestExceptionHandler::external_logging('api');
switch ($avalue) {
case self::THROW_CUSTOM:
// throw custom exception
throw new VerendusException('1 is an invalid parameter', 5);
break;
case self::THROW_DEFAULT:
// throw default one.
throw new Exception('2 is not allowed as a parameter', 6);
break;
default:
// No exception, object will be created.
$this->var = $avalue;
break;
}
}
}
$a = new TestException(TestException::THROW_DEFAULT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment