This article mainly introduces the simple exception handling class implemented by PHP, and analyzes the relevant implementation techniques of PHP's exception handling operations based on object-oriented technology based on specific examples. Friends in need can refer to the following
for details As follows:
<?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; }
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP Example code for strstr function to determine whether a string exists_php basics
The difference between time() and mktime() methods in php_php basics
php array declaration, traversal, array Summary of using global variables_php basics
The above is the detailed content of How to implement exception handling class in PHP. For more information, please follow other related articles on the PHP Chinese website!