[SOLVED] PHP5 Exceptions
Posted: Sat Jul 03, 2004 2:35 am
I believed I understood exceptions, but I guess I was wrong
. I wrote a short script that connects to a database. In the database code, there are Exceptions, which should filter to the error class. The odd thing is, it DOES work. When an error occurs, the error class pickes up the thrown exception and outputs: The thing is, I also recieve a fatal error: Obviously, I want to get rid of this fatal error. Here is my index page (which calls the database class ):
Next is my lib_classes.php file, which includes the database and error class: Any help would be great! Thanks!
Code: Select all
Error 'could not query database' in class 'database' in function 'execute'Code: Select all
Fatal error: Exceptions must be valid objects derived from the Exception base class in /www/openhelp.org/beta/lib_classes.php on line 55Code: Select all
<?php
/*----------------------------------------------------------------------------*/
/*OpenHelp */
/*Index Page - index.php */
/* */
/*Written by Rob Frawley on 2004-05-06 */
/*Last Edited on 2004-07-02 */
/*----------------------------------------------------------------------------*/
/*INCLUDE FILES---------------------------------------------------------------*/
require_once('conf_vars.php');
require_once('lib_classes.php');
/*DEFINE CURRENT PAGE---------------------------------------------------------*/
if (!$_GET['page']) {
$page_current = 'main';
} else {
$page_current = strip_tags(htmlspecialchars($_GET['page']));
}
/*MAKE PAGE-------------------------------------------------------------------*/
$database = new database($_CONFIG['db_user'],
$_CONFIG['db_pass'],
$_CONFIG['db_host'],
$_CONFIG['db_name']);
$database->execute('this');
?>Code: Select all
<?php
<?php
/*----------------------------------------------------------------------------*/
/*OpenHelp */
/*Class Library - lib_classes.php */
/* */
/*Written by Rob Frawley on 2004-05-06 */
/*Last Edited on 2004-07-02 */
/*----------------------------------------------------------------------------*/
/*PAGE CLASS------------------------------------------------------------------*/
class page_default {
}
/*DATABASE CLASS--------------------------------------------------------------*/
class database {
protected $db_user;
protected $db_pass;
protected $db_host;
protected $db_name;
protected $dbh;
public function __construct ($db_user, $db_pass, $db_host, $db_name) {
$this->db_user = $db_user;
$this->db_pass = $db_pass;
$this->db_host = $db_host;
$this->db_name = $db_name;
}
protected function connect() {
$this->dbh = mysql_pconnect ($this->db_host, $this->db_user, $this->db_pass);
if (!is_resource($this->dbh)) {
throw new error (__CLASS__,
__FUNCTION__,
'database connection not a valid resource');
}
if (!mysql_select_db($this->db_name, $this->dbh)) {
throw new error (__CLASS__,
__FUNCTION__,
'could not select database');
}
}
public function execute($query) {
if (!$this->dbh) {
$this->connect();
}
$result = mysql_query($query, $this->dbh);
if (!$result) {
throw new error (__CLASS__,
__FUNCTION__,
'could not query database');
}
else if (!is_resource($result)) {
throw new error (__CLASS__,
__FUNCTION__,
'database result is not a valid resource');
} else {
return $result;
}
}
public function get_row($result) {
return mysql_fetcg_row($result);
}
public function get_assoc($result) {
return mysql_fetch_assoc($result);
}
public function get_array($result) {
return mysql_fetch_array($result);
}
}
/*ERROR CLASS-----------------------------------------------------------------*/
class error {
public function __construct ($error_class, $error_function, $error_msg) {
if (!$error_class) {
$error_class = 'Undefined';
}
if (!$error_function) {
$error_function = 'Undefined';
}
if (!$error_msg) {
$error_msg = 'Undefined';
}
echo '<p>Error '''.$error_msg.''' in class '''.$error_class.''' in function '''.$error_function.'''</p>';
}
}
?>
?>