您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php封装一个异常的处理类的方法

2025/4/1 19:29:51发布22次查看
这篇文章主要为大家详细介绍了php封装一个异常的处理类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
 本文实例为大家分享了php自定义异常处理类,供大家参考,具体内容如下
一、代码
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>自定义异常处理类</title> </head> <body> <?php class telexception extends exception{ //定义telexception类,继承exception类 public function errortel(){ //定义方法返回错误信息 $errormsg = "出错原因:".$this->getmessage()."不是一个合法的电话号码"; $errormsg .="<br>"; $errormsg .="错误文件路径:".$this->getfile(); $errormsg .="<br>"; $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"); } ?> </body> </html> </body> </html>
二、运行结果
相关推荐:
php实现错误处理封装类的方法
php常用操作类之通信数据封装类的方法详解
php mysql数据库如何封装类
以上就是php封装一个异常的处理类的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product