php 可以不用exception,php 用Exception?大家都是这么写的吗?

这是项目中注册的代码:

$mobile = trim($_POST['mobile']);

$code = $_POST['code'];

$password = $_POST['password'];

try {

if(empty($password)) {

throw new \Exception('请填写密码');

}

if(strlen($password) < 6 || preg_match("/^\d+$/", $password)) {

throw new \Exception('密码必须大于6位,并且不能是纯数字');

}

$mobilecode = new Chkcode;

if(!$mobilecode->verify($code, 'phone_code_'.$mobile)) {

throw new \Exception('手机验证码不正确');

}

$user = new User();

if($user->phone_exists($mobile)) {

throw new \Exception('手机号已注册或绑定');

}

$uid = $user->create([

'phone'=>$mobile,

'password'=>$user->hash_password($password),

'create_datetime'=>date('Y-m-d H:i:s')

]);

$ip = $_SERVER['REMOTE_ADDR'];

$user->log_history($uid, $ip, true);

$user->set_online($uid);

return $response->withJson([

'errcode' => 0

]);

} catch (\Exception $e) {

$msg = $e->getMessage();

$code = $e->getCode() || 1;

return $response->withJson([

'errcode' => $code,

'msg' => $msg

]);

}

之前的phper喜欢用Exception,在最下面统一处理异常

这样倒是挺方便的,流程明了,但是性能和return比应该低了一点吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值