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

 
jQuery UI provides a comprehen
 
Program Plan   I drafted a p
 
I present to you my skills, ac
 
Introduction One of the issue
 
If you are a PHP developer and
 
cURL is a great tool to help y
 
cformsII cforms is a powerful
 
  The lack of Unicode su
 
History PHP-GTK was origina
 
Performance on the web is stra
 
Listen t
 
What\'s the number one cost in
 
When you\'re discussing the In
 
Classe
 
A service-oriented architectur
 
Introduc
 
PHP Crons and Linux Linux has
 
Cross site scripting (XSS) is
 
What Makes a Web 2.0 Applicati
 
As you develop web application
 
Cryptogr
 
Posting
 
Have you
 
Resources The Google API - ht
 
Get Started
 
Output B
 
If you r
 
PHP has some really sweet new
 
There were some new php.ini di
 
In PHP 5 there are some new fu
 
The following code implements
 
The following code snippet imp
 
A fine implementation of the o
 
Exception handling PHP 5 adds
 
Support for dereferencing obje
 
Static members Classes defini
 
Explicit object cloning In or
 
final methods The final keywo
 
Interfaces Gives the ability
 
The new object oriented featur
 
Sometimes its the little thing
 
Consider your file is at locat
 
# If a method can be static, d