The MySQLException Class
You can improve the functionality of a class when using inheritance by adding new methods or changing inherited ones. Changing inherited methods of a class is called overriding. In this particular case, the number of changes you can make to existing methods by overriding them is severely limited because, as you saw in Listing 10-1, there are only two non-final methods of the Exception class: the constructor and the_toString method. Let's change both of these methods (see Listing 10-3).
class MySQLException ©extends Exception{ //no new data members public function _construct($message, $errorno){
- check for programmer error if($errorno >= 5000){
- message = __CLASS__ ." type. Improper class usage. ". $message; }else{
- message = __CLASS__ . " - ". $message;
//call the Exception constructor parent::_construct($message, $errorno);
//override __toString public function __toString(){
return ("Error: $this->code - $this->message");
Listing 10-3: Code to create the MySQLException class
To inherit from an existing class and add its functionality to a newly created class, use the keyword extends and the parent class name. Since Exception is a built-in class, there's no need to explicitly include any files. The keyword extends is all that's needed in order to give our newly created class immediate access to all the public and protected methods and data members of its parent. This is a very succinct and elegant way of reusing code.
Overridden Methods
Listing 10-3 shows all the code required to create a class derived from Exception. There are only two methods and both are overridden parent class methods.
But let's take a more detailed look, beginning with the constructor. Note how it checks the value of the error number. This test is designed to separate errors attributable to the programmer from all other errors.
We've chosen the range 5,000 and greater because this range is not used by built-in MySQL errors. The message associated with programmer errors indicates misuse of the class, and differentiating client programmer errors from other errors makes it easier to use the database classes.
For clarity, the error message includes the class name, which we avoid hard-coding by using the constant_CLASS_. After identifying the type of error, the Exception class constructor is called using the scope resolution operator and the keyword parent. (You encountered similar syntax when you referenced a static variable in Chapter 9.) This is the syntax for calling any parent method from within a derived class, and one of the few cases where it's necessary to invoke a magic method directly.
As you can see, there is no need to hard-code the parent class name because all constructors are invoked by calling_construct—the very reason for introducing a magic construction method in PHP 5.
NOTE If a derived class overrides a parent constructor, there is no implicit call to the parent. The call must be made explicitly, as in Listing 10-3.
The_toString method defined in Listing 10-3 replaces the_toString method inherited from the parent class. As a result, a MySQLException echoed to the screen shows only the error number and the associated message, which is much less informative than the_toString method of the parent class
(which traces the error and shows its line number). This makes for more secure production code because it reduces the information associated with an exception, but it also makes development of applications more difficult. (You may want to comment out this code while debugging an application. By so doing, you revert to the more informative method of the parent.)
Post a comment