Changes to the MySQLConnect Class

The changes required so that the MySQLConnect class can use MySQLException objects are minimal. Of course the MySQLConnect class needs to know about this derived exception class, but this is easily accomplished with the following statement:

require 'MySQLException.php';

Next, you need an error code number that is greater than or equal to 5,000 (that is, outside the range used by MySQL). Then define a constant class value using the keyword const and give this constant a name using uppercase letters (per convention). The const keyword performs the same task for OOP as the define function does for procedural programming—it declares a variable that cannot be changed. Constant data members do not use access modifiers, so they are effectively public.

const ONLY_ONE_INSTANCE_ALLOWED = 5000;

The only other changes involve the constructor, as shown in Listing 10-4.

Listing 10-4. Changes to the MySQLConnect constructor
public function __construct($hostname, $username, $password){
    if(MySQLConnect::$instances == 0){
      if(!$this->connection = mysql_connect($hostname, $username,$password )){
       ❶throw new MySQLException(mysql_error(), ❷mysql_errno());
      }
      MySQLConnect::$instances = 1;
    }else{
      $msg = "Close the existing instance of the ".
        "MySQLConnect class.";
      throw new MySQLException(❸$msg, self::ONLY_ONE_INSTANCE_ALLOWED);
    }
}

Compare Listing 10-4 with Listing 10-2. Notice that the calls to the die function have been removed, and an exception has been constructed in their place. The new keyword throw (❶) is used exclusively with exceptions. It hands off the exception to be dealt with elsewhere (as you'll see in the following section).

The first MySQLException is constructed using ❷ the built-in MySQL error number and message. In the second case ❸ an appropriate message is created and the class constant, ONLY_ONE_INSTANCE_ALLOWED, is passed to the constructor. (Notice the syntax for referencing a class constant using the scope resolution operator and the keyword self; this is exactly the same way that a static variable is referenced.)

Prodding Your Class into Action

If you force an exception by attempting to create a second connection without closing the first one, you see this message:

Error: 5000 - MySQLException type. Improper class usage. Close the existing instance
of the MySQLConnect class.

This tells you the class the exception belongs to, that the error results from misuse of the class, and how to rectify the error.

Changes to the MySQLResultSet class are identical to the changes shown above. Constant data members with values greater than 5,000 are added to the class in order to identify class usage errors, but otherwise existing error numbers and messages are used. (We won't deal with the details here; to view those changes, download the files associated with this chapter.)


Note:

Were you to develop the MySQL classes further, you might end up with an unwieldy number of constants. In that case it would make sense to remove constant data members from their respective classes and store them in a file associated with the MySQLException class, or perhaps define them all in the MySQLConnect class, thereby avoiding possible numbering conflicts.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.118.95