SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    England
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    combining classes - long post

    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 ( 
    == $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);
    }
      


  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't 'extends' do it?

    Edit:

    Oh, you're new, sorry.

    class DB extends errorReporting
    {
    }

    Instead of:

    class DB
    {
    }

    Then from DB you can go $this->error_reporting_functions. It makes DB inherit the errorReporting functions.

  3. #3
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would pass it in the constructor since the DB class has nothing to do with the error class and should be able to access and view it's internals.
    PHP Code:
    class DB_Sql {
        function 
    DB_Sql (&$error_reporter) {
            if (
    is_a($error_reporter'errorReporter')) {
                
    $this->errordb =& $error_reporter;
            }
        }
        
    // ...
    }

    // Example
    $error_reporter =& new errorReporter();
    $db =& new DB_Sql($error_reporter); 

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ..or that'll work too.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    England
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers guys. I think that second method looks most like what I had in mind because I also use the error reporter outside the database object as well.

  6. #6
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, if you use it outside second method is better.

    With the extend you can just go $this->error_reporting_functions as opposed to like $this->error->error_reporting_functions.. but extends are more specifically used if you only use it in that class.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •