Question

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 />\n";';
        }

    } else {
        exit("FATAL ERROR: 101.".$controller."-".$action);
    }
Was it helpful?

Solution

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 />\n"');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top