Reputation: 1499
I know this question has been asked before numorous times but they seem to have a desktop application background and not web application. I am currently building a web application using PHP. I'm going to use a controller and model file to demonstrate my question.
Controller file
This file calls upon the database to query and fetch data
<?php public function index() {
$this->database->query("SELECT user_name, FROM test WHERE user_name = :user_name");
$this->database->execute_query("jim");
$this->view->data = $this->database->fetch_query();
?>
The database file
<?php
class DB {
private $datasourcename;
private $user;
private $password;
private $connection;
private $prepare;
private $query;
function __construct($dsn, $user, $password) {
$this->datasourcename = $dsn;
$this->user = $user;
$this->password = $password;
$this->connection = new PDO($this->datasourcename, $this->user, $this->password);
}
public function query($query) {
$this->query = $query;
try {
if (empty($query)) {
throw new Exception("The query is empty");
return false;
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "<br/>";
}
if (strstr($query, ":") == FALSE) {
return $this->connection->query($query);
} else {
$this->prepare = $this->connection->prepare($query);
}
}
public function execute_query($valarg = array()) {
try {
if (empty($valarg)) {
throw new Exception("There are no values in the execute query function");
return false;
}
if (is_array($valarg) == false) {
throw new Exception("The values inserted are not in an array");
return false;
} else {
$query = $this->query;
$paramkeys = array();
$paramArr = array();
if (strstr($query, ":")) {
preg_match_all("/:(\w+)/", $query, $paramkeys);
$paramArr = array_combine($paramkeys[0], $valarg);
$this->prepare->execute($paramArr);
}
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "<br/>";
}
}
public function fetch_query() {
try {
if($this->execute_query() == false) {
throw new Exception("Sorry you need to fix this first");
}
if($this->query() == false) {
throw new Exception("Sorry you need to fix this first");
}
else {
$result = $this->prepare->fetch(PDO::FETCH_ASSOC);
return $result;
}
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "<br/>";
}
}
}
?>
I have used exception handling in my database class as a way for a developer to use the functions provided by that class correctly in order for the application to work, am I right im thinking that this is the best practise to use them? Is there a clear cut best practise when and how to use them? I have read similar questions on this site but the background was desktop application design and not web application.
Upvotes: 1
Views: 674
Reputation: 163232
Web programming is no different than desktop programming in this regard.
Use exceptions for exceptional events. Use return values for anything expected.
I think seeing an empty query result isn't something that will have an exception thrown, generally. I'd just return no data. If the database query failed somehow, that would be a reason to throw an exception. Also, your exception messages should probably be more descriptive, rather than "you need to fix this".
Finally, in PHP 5.3, you can set an inner exception by passing it as the third parameter in the Exception
constructor.
http://www.php.net/manual/en/class.exception.php
Upvotes: 6