首页  >  文章  >  后端开发  >  PHP实现异常处理类的方法

PHP实现异常处理类的方法

墨辰丷
墨辰丷原创
2018-05-23 10:51:161529浏览

这篇文章主要介绍了PHP实现的简单异常处理类,结合具体实例形式分析了php基于面向对象技术实现异常处理操作的相关实现技巧,需要的朋友可以参考下

具体如下:

<?php
header(&#39;content-type:text/html;charset=UTF-8&#39;);
// 创建email异常处理类
class emailException extends exception
{
}
// 创建pwd异常处理类
class pwdException extends exception
{
  public function __tostring(){
    return $this->getMessage().&#39;in file:&#39;.$this->getFile().&#39;on line:&#39;.$this->getLine();
  }
}
function reg($reginfo = null)
{
  // 依据不同错误抛出不同异常
  if (empty($reginfo) || !isset($reginfo)) {
    throw new Exception(&#39;参数非法&#39;);
  }
  if (empty($reginfo[&#39;email&#39;])) {
    throw new emailException(&#39;邮件为空&#39;);
  }
  if ($reginfo[&#39;pwd&#39;] != $reginfo[&#39;repwd&#39;]) {
    throw new pwdException(&#39;两次密码不一致!&#39;);
  }
}
// 接收不同异常,并针对性处理!
try {
  reg(array(&#39;email&#39; => &#39;1078789950@qq.com&#39;, &#39;pwd&#39; => &#39;123&#39;, &#39;repwd&#39; => &#39;1231&#39; ));
} catch (Exception $e) {
  echo $e ->getMessage();
} catch (emailException $ee) {
  echo $ee ->getMessage();
} catch (pwdException $ep) {
  echo $ep;
}

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

PHP strstr 函数判断字符串是否否存在的实例代码_php基础

php中time()和mktime()方法的区别_php基础

php数组声明、遍历、数组全局变量使用小结_php基础

以上是PHP实现异常处理类的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn