我对 PHP 和自定义错误处理程序的设置相当陌生,但是我有一些代码在 CI 之外可以正常工作,但我不知道如何将其集成到 CI 控制器中。我收到一个错误“消息:未定义的属性:错误::$my_error_handler”
我的控制器是:
<?php
class Errors extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function my_error_handler($number, $message, $file, $line, $vars)
{
$email = "
<p>An error ($number) occurred on line
<strong>$line</strong> and in the <strong>file: $file.</strong>
<p> $message </p>";
$email .= "<pre>" . print_r($vars, 1) . "</pre>";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Email the error to someone...
error_log($email, 1, 'name@domain.com', $headers);
// Make sure that you decide how to respond to errors (on the user's side)
// Either echo an error message, or kill the entire project. Up to you...
// The code below ensures that we only "die" if the error was more than
// just a NOTICE.
if ( ($number !== E_NOTICE) && ($number < 2048) ) {
die("There was an error. Please try again later.");
}
}
function test()
{
// We should use our custom function to handle errors.
set_error_handler($this->my_error_handler);
// Trigger an error... (var doesn't exist)
echo $somevarthatdoesnotexist;
}
}
?>
如果有更好的方法使用 CI 通过电子邮件发送错误消息,请告诉我。