Before we start building the second component of the MySQL driver mentioned in the introduction, it’d be really helpful to reintroduce the definition of the first class that comprises this driver. Among other tasks, it connects to the database server, runs SQL queries and so forth via a Singleton object. The full source code of this class is as follows: class MySQLiWrapper extends MySQLi {
private static $_instance = NULL; private $_config = array();
// return Singleton instance of MySQL class public static function getInstance(array $config = array()) { if (self::$_instance === NULL) { self::$_instance = new self($config); } return self::$_instance; }
// private constructor private function __construct(array $config) { if (count($config) < 4) { throw new Exception('Invalid number of connection parameters'); } $this->_config = $config;
}
// prevent cloning class instance private function __clone(){}
// establish a connection to MySQL public function connect() { list($host, $user, $password, $database) = $this->_config; parent::__construct($host, $user, $password, $database); if ($this->connect_error) { throw new Exception('Error connecting to MySQL : ' . $this->connect_errno . ' ' . $this->connect_error); } unset($host, $password, $database); } // perform query public function runQuery($query) { if (is_string($query) AND !empty($query)) { // lazy connect to MySQL $this->connect(); // run the specified query if ((!$this->real_query($query))) { throw new Exception('Error performing query ' . $query . ' - Error message : ' . $this->error); } return new MySQLi_ResultWrapper($this); } }
// get insertion ID public function getInsertID() { return $this->insert_id; }
// close database connection public function __destruct() { $this->close(); } } As I said before, the above “MySQLiWrapper” class performs a few simple tasks, such as opening and closing a connection to MySQL, executing hard-coded SQL statements and so forth. While the respective implementations of the methods that carry out all these operations do deserve further analysis, the most important detail to stress here is the use of the static $_instance property within the “getInstance()” method. It returns to client code a Singleton instance of the class. This method itself demonstrates a concrete use of a static variable that not only improves the behavior of its containing class, but also boots the performance of the application using the class in question. And now that you’ve recalled the inner workings of the previous “MySQLiWrapper” driver, it’s time to start defining the class that iterates over result sets, since an object spawned from this class is created within the corresponding “query()” method. As you’ll see in a few moments, this whole new class will be an implementer of the Iterator and Countable PHP 5 native interfaces. Its definition will be shown in the section to come. Therefore, to get there click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|