I have an error reporting class and a database class (that I've taken off the web and modified a bit). I want to use my error reporting class to generate error messages within my database class as well as in the rest of the procedural code I have written. How do I do this?
Sorry but I'm new to this classes thing, apologies if this is in the wrong forum section but I thought its basic OOP so it should go here
PHP Code:class errorReporter
{
// define main error types
// < 1000 are fatal (script stops immediately)
// > 1000 are non-fatal (script stops once all non-fatals have been processed)
var $_errorTypes = array(
100 => "Database error",
200 => "File I/O error",
300 => "Authentication error",
400 => "Script error",
500 => "User error",
1100 => "Form error",
1200 => "URL error"
);
var $_errorsubTypes = array(
101 => "Bad data",
201 => "Could not read file",
301 => "Session expired",
401 => "Invalid query",
1101 => "Form error",
1201 => "URL error"
);
// constructor
function errorReporter()
{
// initialize error stack
$this->flushErrors();
// start output buffering
ob_start();
}
// clean stack
function flushErrors()
{
$this->_errors = array();
return $this->_errors;
}
// manually raise an error
function raiseError($subtype=NULL, $data="No additional information available")
{
$this->_errors[] = array("subtype" => $subtype, "data"=> $data);
if ($this->_getErrorType($subtype) < 1000)
{
// fatal
$this->displayFatalError($subtype, $data);
}
return $this->_errors;
}
// internal function to map subtype to primary type
function _getErrorType($subtype)
{
return floor($subtype/10)*10;
}
// fatal error handler
// clear previously-drawn screen
// display error template with message
// stop script processing and die()
function displayFatalError($subtype, $data)
{
ob_clean();
$errorType =$this->_errorTypes[$this->_getErrorType($subtype)];
$errorCode = $subtype;
$errorMsg = $this->_errorsubTypes[$subtype];
$addInfo = $data;
include("error-fatal.php");
die;
}
// non-fatal error handler
// clear previously-drawn screen
// build an array of non-fatal errors
// and then die()
function displayNonFatalErrors()
{
ob_clean();
$errorList = array();
for($x=0; $x<$this->numErrors(); $x++)
{
$errorType = $this->_errorTypes[$this->_getErrorType($this->_errors[$x]["subtype"])];
$errorCode = $this->_errors[$x]["subtype"];
$errorMsg = $this->_errorsubTypes[$this->_errors[$x]["subtype"]];
$addInfo = $this->_errors[$x]["data"];
$errorList[] = array($errorType, $errorCode, $errorMsg, $addInfo);
}
include("error-non-fatal.php");
die;
}
};
class DB_Sql {
var $Host = "localhost"; // Hostname of the MySQL server.
var $Database = "coweb"; // Logical database name on that server.
var $User = "root"; // User and Password for login.
var $Password = "triadpass";
var $Link_ID = 0; // Result of mysql_connect().
var $Query_ID = 0; // Result of most recent mysql_query().
var $Record = array(); // current mysql_fetch_array()-result.
var $Row; // current row number.
var $Errno = 0; // error state of query...
var $Error = "";
// insert functions here.
function halt($msg) {
//I want to raise a fatal error using the error reporting class here i.e.
$errordb->raiseError(100,$msg);
//What was used before (at least it worked!)
/*
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->Errno,
$this->Error);
die("Session halted.");
*/
}
function connect() {
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mysql_connect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID) {
$this->halt("Link-ID == false, connect failed");
}
if (!mysql_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
}
}
}
function query($Query_String) {
$this->connect();
# printf("Debug: query = %s<br>n", $Query_String);
$this->Query_ID = mysql_query($Query_String,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->Query_ID) {
$this->halt("Invalid SQL: ");
}
$this->next_record();
return $this->Query_ID;
}
function next_record() {
$this->Record = mysql_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$stat = is_array($this->Record);
if (!$stat) {
mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $stat;
}
function seek($pos) {
$status = mysql_data_seek($this->Query_ID, $pos);
if ($status)
$this->Row = $pos;
return;
}
function num_rows() {
return mysql_num_rows($this->Query_ID);
}
function disconnect(){
mysql_close($this->Link_ID);
}
}






Bookmarks