Exception Handling - By PHP Expert


PHP has some really sweet new features and one of them is the ability to throw and catch exceptions.
When I was browsing Sitepoint I read a post saying that the only thing they need to do is get rid of the class-"hinting" in the catch clause. I guess they didn't get the point of extending the base Exception class.
I've found extending the Exception class useful when reporting the errors back from a Database connection object. Basically I wanted to return a message array to describe the problem in English, then with additional messages from MySQL. This is really just so I can separate HTML from the PHP, for instance I could just return "Could not connect to database: " . mysql_error() and be done with it, but if a design changes and strong isn't applicable or something, it's not that much good. Ok, so perhaps I'm just being anally-retentive but it's cool to demonstrate anyway.
class DatabaseException extends Exception
{
  private $error;    (1)

  function __construct($error) (2)
  {
    $this->error = $error;
  }

  function getMessageArray()
  {
    return $this->error;
  }

}
1.    Yup, you use either "public" "private" or "protected" when declaring variables now. Unfortunately they're not C++ style-so you have to use them for each variable.
2.    Also you use __construct for constructors.
Next the simple database class:
class Database
{
  private $host;
  private $password;
  private $user;
  private $database;
  public $connection_tracker;

  function __construct($host, $user, $password, $database)
  {
    $this->host = $host;
    $this->password = $password;
    $this->user = $user;
    $this->database = $database;
    $this->connect();
  }

  function connect()
  {
    if (!$this->connection_tracker = @mysql_connect($this->host, $this->user, $this->password))
    {
      $error_array =  array ( (1)
      "message" => "Could not connect to MySQL daemon",
      "database_reports" => mysql_error()
      );

      throw new DatabaseException($error_array);
    }
  }
}
1.    Passing the array of messages we want to the class to access:
try
{
  $Database = new Database('localhost', 'rot', 'discovery', 'table'); (1)
}

catch (DatabaseException $e)
{
  $error_messages = $e->getMessageArray();
  echo $error_messages['message'];
  echo $error_messages['database_reports'];
}
1.    Username supposed to be root.
Pretty cool eh? Now you can put the HTML around it as required by the design. Anally retentive, sure, but I'm sure you can think of some other uses from extending the Exception class

No comments

Enter your email address:

Delivered by FeedBurner

OR

 Subscribe in a reader

 
 
 
Tips for optimizing php code by PHP Expert Important SERVER Variables in PHP - By PHP Expert Improved Error Messages in PHP 5 - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert Object Overloading in PHP 5 Persistable Classes - By PHP Expert Dynamic Getter/Setter Methods - By PHP Expert New Functions in PHP 5 New Directives - By PHP Expert Exception Handling - By PHP Expert Password Encryption in PHP - By PHP Expert Output Buffering in PHP - By PHP Expert Page Excerpts Using CURL - By PHP Expert Quick and Easy Google Site Search - By PHP Expert Always Be Notified When Google Crawls Your Site - By PHP Expert How to POST Form Data using CURL - By PHP Expert Cryptography for web developers - By PHP Expert Dynamically Loading JavaScript Files - by PHP Expert What is Web 2.0 - By PHP Expert