PHPで例外(exceptions)処理サンプル

PHPコード:
<?php
//エラー出力を禁止
error_reporting(0);
//エラーハンドラを設定
set_error_handler('errorHandler’);
register_shutdown_function('fatalErrorHandler’);
class Test{
public function index(){
echo $undefinedVarible;
}
}
function errorHandler($errno,$errstr,$errfile,$errline){
$arr = array(
'['.date('Y-m-d h-i-s’).’]’,
'http://www.google.co.jp’,
'|’,
$errstr,
$errfile,
'line:’.$errline,
);
//エラーログに書き込む
error_log(implode(' ',$arr)."\r\n",3,’./log_startnews24.txt’,’extra’);
echo implode(' ',$arr)."\r\n";
}
//catch fatalError
function fatalErrorHandler(){
$e = error_get_last();
switch($e['type’]){
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
errorHandler($e['type’],$e['message’],$e['file’],$e['line’]);
break;
}
}
$test = new Test();
//errorHandlerで警告エラーを捕獲
$test->index();
//致命的なエラー
$test = new Tesdt();
$test->index();

PHP

Posted by arkgame