I basically want to take the errors raised by PHP, turn them into an equivalent exception, and have it passed to my normal exception handler. However, I am getting some behavior that I don't understand.
I'm running Apache/2.2.3 (Linux/SUSE), with PHP Version 5.2.0.
Here is the code I am running (note, there are a few extraneous things here that aren't specifically related to the test):
<?php
class WE_Exception extends Exception
{
protected $fatal;
public function __construct($fatal, $message) { parent::__construct($message); $this->fatal = ($fatal) ? TRUE : FALSE; }
public function handle() { print "\ninto WE_Exception->handle() method."; }
}
class WE_PHPError extends WE_Exception
{
public function __construct($errno, $errstr, $errfile, $errline)
{
parent::__construct(TRUE, "PHP Error [".$errno."]: ".$errstr);
$this->file = $errfile;
$this->line = $errline;
}
}
function WE_exceptionHandler($exception) { print "\ninto WE_exceptionHandler"; $exception->handle(); }
function WE_errorHandler($errno, $errstr, $errfile, $errline)
{ print "\ninto WE_errorHandler"; throw new WE_PHPError($errno, $errstr, $errfile, $errline); return; }
set_error_handler("WE_errorHandler");
set_exception_handler("WE_exceptionHandler");
print "<pre>\n";
include("/non/existent/file");
print "\nin between include() and require() calls";
require("/non/existent/file");
print "\n</pre>";
?>
The script outputs:
into WE_errorHandler
into WE_exceptionHandler
into WE_Exception->handle() method.
I expected this to continue on and process the require() call (from PHP site: "Also note that it is your responsibility to die() if necessary. If the error-handler function returns, script execution will continue with the next statement after the one that caused an error.").
Finally, if the include() call is commented out, the output is:
in between include() and require() calls
into WE_errorHandler
Fatal error: main(): Failed opening required '/non/existent/file' (include_path='.:/usr/share/php5:/usr/share/php5/PEAR') in /srv/www/htdocs/website/test.php on line 37
It appears that the exception handler is never being called. I checked, and the WE_Exception constructor is getting called, but for some reason, the exception handler is not being called after it is constructed.
I would appreciate someone's insight into this as I have been stepping through my code and the related documentation and (obviously) am confused about some aspect of PHP's error/exception handling mechanisms.