php封装一个异常的处理类
发布时间:2020-12-12 22:10:34 所属栏目:PHP教程 来源:网络整理
导读:一、代码 自定义异常处理类 getMessage()."不是一个合法的电话号码"; $errorMsg .=" "; $errorMsg .="错误文件路径:".$this->getFile(); $errorMsg .=" "; $errorMsg .="错误代码行号:".$this-> getLine(); return $errorMsg; } } function check_tel($tel
一、代码
自定义异常处理类
getMessage()."不是一个合法的电话号码";
$errorMsg .=" ";
$errorMsg .="错误文件路径:".$this->getFile();
$errorMsg .=" ";
$errorMsg .="错误代码行号:".$this-> getLine();
return $errorMsg;
}
}
function check_tel($tel){ //自定义函数验证电话号码格式是否正确
$checkphone="/^13(d{9})$/"; //定义验证手机号码的正则表达式
$counts=preg_match($checkphone,$tel); //执行验证操作
return $counts; //返回验证结果
}
$tel = "133891gfj"; //定义被验证的电话号码
/*
通过自定义异常处理类返回错误提示
*/
try {
if(check_tel($tel) !=1){
throw new TelException($tel);
}
}catch (TelException $e){
include_once("error.php");
}
?>
|