Universal error handler
The process of developing a universal error handler is quite similar to the preceding recipe. There are certain differences, however. First of all, in PHP 7, some errors are thrown and can be caught, whereas others simply stop your application dead in its tracks. To further confuse matters, some errors are treated like exceptions, whereas others are derived from the new PHP 7 Error
class. Fortunately for us, in PHP 7, both Error
and Exception
implement a new interface called Throwable
. Accordingly, if you are not sure whether your code will throw an Exception
or an Error
, simply catch an instance of Throwable
and you'll catch both.
How to do it...
- Modify the
Application\Error\Handler
class defined in the preceding recipe. In the constructor, set a newerrorHandler()
method as the default error handler:public function __construct($logFileDir = NULL, $logFile = NULL) { $logFile = $logFile ?? date('Ymd') . '.log'; $logFileDir = $logFileDir...