The MySQLConnect Class

The MySQLConnect class is a fairly modest class with only two data members and four public methods.

//data members
private $connection
private static $instances = 0
//methods
public function __construct($hostname, $username, $password)
public function __destruct()
public function createResultSet($strSQL, $databasename)
public function close()

What is immediately noteworthy about this class is the use of the keyword static to modify a data member. Identifying a variable as static means that it is shared among all instances of a class. If one instance changes the value of a static variable, it is changed for all instances. Unique variables are created each time a class is instantiated, and they belong to that specific instance. Not so with static variables—they belong to the class as a whole (and for this reason are sometimes referred to as class variables). Let's look at the code for the constructor and see how this can be useful.

A Class-Conscious Variable

The parameters passed to the constructor are those necessary to make a database connection using the built-in PHP function, mysql_connect. Again, this method is a wrapper method but with a few additional bells and whistles.

public function __construct($hostname, $username, $password){
    if(❶MySQLConnect::$instances == 0){
        $this->connection = mysql_connect($hostname,$username,$password) or
        die ( mysql_error(). " Error no:".mysql_errno());
        MySQLConnect::$instances = 1;
   }else{
        $msg = "Close the existing instance of the ".
        "MySQLConnect class.";
        die($msg);
    }
}

This class won't be instantiated if there is already an existing instance. If the $instances variable has a value of 0, a connection is made to the server, and the value of $instances is set to 1. Checking the value of this static variable makes sense because it is shared among all instances, it's available to all instances, and its value is the same for all instances.

The syntax for referencing a static variable (❶) is different from that used to reference a normal data member. It would not make sense to use the pseudo-variable $this with $instances, since $this refers to the current object and by definition, static variables belong to the class rather than a specific instance. Quite sensibly, the class name is used instead, and the arrow operator is replaced by a double colon—the scope resolution operator.

The scope resolution operator is principally used when referencing static data members or static methods. In Chapter 10 you'll see the two other occasions when this operator is used, but for now you need only concern yourself with its use with static data members. When referencing a static variable from within its class, you also have the option of replacing the class name with the keyword self. In this case, the expression self::$instances is equivalent to MySQLConnect::$instances. Static members referenced outside the confines of their class must use the class name. You don't need to worry about that here, since $instances is private and cannot be referenced outside the MySQLConnect class.

At this point you may be thinking, "That's all well and good, but why would I want a class that I can only create one instance of?" Creating a database connection is an expensive operation so restricting creation of connections conserves resources.


Note:

By restricting the connection class to a single instance, we are mimicking the built-in mysql_connect function. Its default behavior is to reuse a connection resource rather than create a new one.


However, there are some circumstances where a new connection is a necessity.

Making Other Connections

Two different connection objects are required if a single script needs to connect to two different servers. The close method makes it possible to connect to a different server.

public function close(){
    MySQLConnect::$instances = 0;
    if(isset($this->connection)){
        mysql_close($this->connection);
        unset($this->connection);
    }
}

Two instances of the MySQLConnect class can exist, but not simultaneously. If you want to create a connection to another server, you must first close the existing connection. The close method closes the current connection and resets the static variable $instances to 0. Manipulating the $instances variable in this way allows you to create a new connection, but only after the current one is closed.

Explicitly closing a connection and unsetting it makes for clearer error messages should you accidentally call a result set method after closing its connection. The requirement to close the current connection also serves as a reminder that a result set is a dependent object.

To make this even clearer, let's look at how a result set is created.

You Can Only Get There from Here

The following method serves as a very strong reminder that you first need a connection in order to create a result set:

public function createResultSet($strSQL, $databasename){
    $rs = new MySQLResultSet($strSQL, $databasename, $this->connection );
    return $rs;
}

The creation of a MySQLResultSet requires a reference to the connection data member of the MySQLConnect class. This data member is private and does not have an accessor method, so it's only available from within the MySQLConnect class. Short of reverting to procedural programming to create a connection resource, you cannot create an instance of the MySQLResultSet class except by using this method of the MySQLConnect class. This makes it very clear that a result set is a dependent object. You can't create one without first having a connection to a server. Instantiating an object of the MySQLResultSet class from within the MySQLConnect class serves not only to remind you of this dependency, but it enforces it programmatically. To understand the connection class, you've had to look ahead at the constructor for the result set class.

Let's examine the rest of this class in detail.

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

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