我正在为我的应用程序编写一个用户定义的PHP异常处理程序,并希望它以不同的方式处理不同类型的异常。
例如,如果应用程序抛出一个未捕获的PDOException,我的处理程序将给我发送一封电子邮件,但如果抛出一个未捕获的异常,将执行另一个操作。
当前处理程序是这样的:
function exception_handler($po_exception) {
// If this is a PDO Exception send an email.
example_email_function('There was a database problem', $po_exception->getMessage());
// If this is any other type of Exception, let the user know something has gone wrong.
echo "Something went wrong.'n";
}
http://www.php.net/manual/en/language.operators.type.php
但是,我建议不要这样粗心的行为。
如果你想监控你的网站是否在运行,只需使用一些外部服务。
对于所有偶然的错误,只需监视错误日志。
另外,不要使用getMessage(),而是使用$po_exception本身。
要确认,回答我的问题的解决方案在@YourCommonSense: http://www.php.net/manual/en/language.operators.type.php提供的链接中,结果代码是:
function exception_handler($po_exception) {
if ($po_exception instanceof PDOException) {
// If this is a PDO Exception, pass it to the SQL error handler.
example_email_function('There was a database problem', $po_exception->getMessage());
}
else {
// Do non database Exception handling here.
}
// If this is any other type of Exception, let the user know something has gone wrong.
echo "Something went wrong.'n";
}