Starting from an idea of @lastguest, I'm writing this code to allow a chain of handlers.
What's the problem? Main problem is that more than one exception handlers aren't allowed so easily.
Code is started when you include the script, converts errors in exceptions and gets only the last exceptions handler.
<?php
set_exception_handler(function () { echo 0; });
set_exception_handler(function () { echo 1; });
$handlers = require_once('handlers.php');
$handlers(function () { echo 2; });
$handlers(function () { echo 3; });
throw new Exception;
// Output: 123
?>
a function is appended to other exceptions handlers by default, but you can prepend it settings the third parameter to false
<?php
$handlers = require_once('handlers.php');
$handlers(function () { echo 1; });
$handlers(function () { echo 2; });
$handlers(function () { echo 3; }, false);
$handlers(function () { echo 4; });
throw new Exception;
// Output: 3124
?>
I don't want to start a discussion (but I would be happy to read your comments and opinions). I "redirected" error to exceptions to work with only one type of objects.