这里有新鲜出炉的 PHP 教程,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
这篇文章主要介绍了 PHP 实现的简单异常处理类, 结合具体实例形式分析了 php 基于面向对象技术实现异常处理操作的相关实现技巧, 需要的朋友可以参考下
本文实例讲述了 PHP 实现的简单异常处理类。分享给大家供大家参考,具体如下:
- <?php
- header('content-type:text/html;charset=UTF-8');
- // 创建email异常处理类
- class emailException extends exception
- {
- }
- // 创建pwd异常处理类
- class pwdException extends exception
- {
- public function __tostring(){
- return $this->getMessage().'in file:'.$this->getFile().'on line:'.$this->getLine();
- }
- }
- function reg($reginfo = null)
- {
- // 依据不同错误抛出不同异常
- if (empty($reginfo) || !isset($reginfo)) {
- throw new Exception('参数非法');
- }
- if (empty($reginfo['email'])) {
- throw new emailException('邮件为空');
- }
- if ($reginfo['pwd'] != $reginfo['repwd']) {
- throw new pwdException('两次密码不一致!');
- }
- }
- // 接收不同异常,并针对性处理!
- try {
- reg(array('email' => '1078789950@qq.com', 'pwd' => '123', 'repwd' => '1231' ));
- } catch (Exception $e) {
- echo $e ->getMessage();
- } catch (emailException $ee) {
- echo $ee ->getMessage();
- } catch (pwdException $ep) {
- echo $ep;
- }
希望本文所述对大家 PHP 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0809/339981.html