標籤:exception 實現 set return wan lin nbsp php har
具體如下:
<?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‘ => ‘[email protected]‘, ‘pwd‘ => ‘123‘, ‘repwd‘ => ‘1231‘ ));
} catch (Exception $e) {
echo $e ->getMessage();
} catch (emailException $ee) {
echo $ee ->getMessage();
} catch (pwdException $ep) {
echo $ep;
}
北京網站建設
PHP實現的簡單異常處理類樣本