Skip to main content
more coding information and spelling error
Source Link
Eric
  • 143
  • 6

What is considered best practicingOr, would it be better to have a "catch-all" in terms of placement ofmy index.php (I use MVC) to catch general exceptions (for example, PDO related), and then if I need more specific ones in my application (for example, those thrown when connecting to 3rd party services), then add additional try-block catches for them: inside of a wrapper class or within the app specific logic?

In my index.php, which all requests pass through via my .htaccess file:

try { $db = new Db(); $db->connect(['host'=>DB_HOST,'dbname'=>DB_NAME,'username'=>DB_USERNAME,'password'=>DB_PASSWORD]); $bootstrap = new Bootstrap(); $bootstrap->setControllerPath('application/controllers'); $bootstrap->setModelPath('application/models'); $bootstrap->init(); //load the controller/model/classes } (catch Exception $e) { //log error //if ajax request return json_encoded message other redirect to error page } 

What is considered best practicing in terms of placement of try-block catches: inside of a wrapper class or within the app specific logic?

Or, would it be better to have a "catch-all" in my index.php (I use MVC) to catch general exceptions (for example, PDO related), and then if I need more specific ones in my application (for example, those thrown when connecting to 3rd party services), then add additional try-block catches for them:

In my index.php, which all requests pass through via my .htaccess file:

try { $db = new Db(); $db->connect(['host'=>DB_HOST,'dbname'=>DB_NAME,'username'=>DB_USERNAME,'password'=>DB_PASSWORD]); $bootstrap = new Bootstrap(); $bootstrap->setControllerPath('application/controllers'); $bootstrap->setModelPath('application/models'); $bootstrap->init(); //load the controller/model/classes } (catch Exception $e) { //log error //if ajax request return json_encoded message other redirect to error page } 
Source Link
Eric
  • 143
  • 6

Where should PDOExceptions be dealt with?

I wrote a database wrapper class to help with queries and I'm trying to figure the best way to implement try-catch blocks.

In my database class, I have a helper function;

public function fetchAll($sql, $bind = array()) { $stmt = $this->query($sql, $bind); $result = $stmt->fetchAll($this->_fetch_mode); return $result; } 

that I can call outside of the class:

$this->db->fetchAll($sql,[$var1,$var2]); 

My question is where to put the try-catch blocks. It would be "easier" if I just put them inside of my database class:

 public function fetchAll($sql, $bind = array()) { $stmt = $this->query($sql, $bind); try { $result = $stmt->fetchAll($this->_fetch_mode); } catch (PDOException $e) { //log the error...give some message } return $result; } 

instead of wrapping each call to the database class:

try { $this->db->fetchAll($sql,[$var1,$var2]); } (catch PDOException $e) { //log the error...give some message } 

However, my feeling (without being able to come up with a concrete example...) is that even though it would be more work to wrap each call to the database class, if I do hard code the try-catch block in my database class, it won't be as flexible in terms of handling errors.

What is considered best practicing in terms of placement of try-block catches: inside of a wrapper class or within the app specific logic?