dqcwl02022 2014-01-31 13:58
浏览 33
已采纳

异常捕获和CallHook

I'm using a callhook to execute my classes and methods in my MVC framework. No i would like to add so error-handling with the PHP Exception Function. I'm just wondering where to it is the best place to execute the catch command. A request can (of course) lead to the execution of multiple classes. Throughout the system exceptions are made. (example is mentioned below).

function callHook() {
    global $urlArray;
    //DEFINE CONTROLLERS
    if (strlen(strstr($urlArray[0],'popup_'))>0)
    {
        $controller = substr($urlArray[0], 6);
    }
    else
    {
        $controller = $urlArray[0]; 
    }
    $queryString[] = $urlArray[1];
    $URLaction = $urlArray[2];

    if(!isset($controller) && empty($controller)){ $controller = 'home';}
    if(!isset($URLaction) || empty($URLaction)){$action = 'view';}else{$action = $URLaction;}

    $controllerName = str_replace('-','', $controller);
    $controller = ucwords($controller);
    $model = rtrim($controller, 's');
    $controller .= 'Controller';
    $dispatch = new $controller($model,$controllerName,$action);

    if ((int)method_exists($controller, $action)) {
        $ResultArray = call_user_func_array(array($dispatch,$action),$queryString);
        return $ResultArray;
    } else {
        exit("FATAL ERROR: 101.".$controller."-".$action);
    }
}

Example Class:

public function CheckCarExistance(){
    if(!is_object($this-> carId)){throw new Exception("carId is missing!");}
        $CountCars = new modelmysql();
        $CountCars->connect();
        $CountCars->count('system_cars', "carId = '".mysql_real_escape_string($this-> carId)."'");
        $this->results = $CountCars ->getResult();

}   

To display all the exceptions would it be a good idea to place the try/catch in the call hook or just in every class/method?

Callhook

if ((int)method_exists($controller, $action)) {
        try{
            $ResultArray = call_user_func_array(array($dispatch,$action),$queryString);
            return $ResultArray;
        }
        catch(Exception $e){
          echo 'Error Found message: ' .$e->getMessage() .' <br />
";';
        }

    } else {
        exit("FATAL ERROR: 101.".$controller."-".$action);
    }
  • 写回答

1条回答 默认 最新

  • doushi1960 2014-01-31 14:08
    关注

    So I would do in this way

    try{
        if ((int)method_exists($controller, $action)) {
            throw new Exception("FATAL ERROR: 101.".$controller."-".$action);
        }
        $ResultArray = call_user_func_array(array($dispatch,$action),$queryString);
        return $ResultArray;
    } catch(Exception $e){
       exit( 'FATAL ERROR: ' .$e->getMessage() .' <br />
    "');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计