As you may know, any modern MVC-driven framework must be at least capable of using a RDBMS for storing data, and the one that I’m building here won’t be the exception. For this particular project, there will be a model class that will handle specifically the data layer, but this model will use an aggregated abstraction class to interact directly with MySQL. Based on these requirements, it’s necessary to build that abstraction class, so below I coded one that performs quite decently. Look at it, please: <?php class MySQL { private $result = NULL; private $link = NULL; private static $instance = NULL; // 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; }
// constructor public function __construct(array $config = array()) { // grab connection parameters list($host, $user, $password, $database) = $config; if ((!$this->link = mysqli_connect($host, $user, $password, $database))) { throw new Exception('Error connecting to MySQL : ' . mysqli_connect_error()); } } // perform query public function query($query) { if (is_string($query) and !empty($query)) { if ((!$this->result = mysqli_query($this->link, $query))) { throw new Exception('Error performing query ' . $query . ' Message : ' . mysqli_error($this->link)); } } }
// fetch row from result set public function fetch() { if ((!$row = mysqli_fetch_object($this->result))) { mysqli_free_result($this->result); return FALSE; } return $row; } // get insertion ID public function getInsertID() { if ($this->result !== NULL) { return mysqli_insert_id($this->link); } return FALSE; }
// count rows in result set public function countRows() { if ($this->result !== NULL) { return mysqli_num_rows($this->result); } return 0; }
// close the database connection function __destruct() { is_resource($this->link) and mysqli_close($this->link); } }// End MySQL class I'm not trying to market to you the above “MySQL” class, because that would be a waste of time. However, it’s fair to say that it works pretty well; it implements a few straightforward methods for running queries, fetching and counting rows in datasets, and finding insertion IDs. That's not too bad, considering that this is only an example. Regardless of the logic implemented by this MySQL abstraction class, which can be more or less complex, the most important thing to note here is that it provides the functionality required by the framework to access database tables via a simple and intuitive API. The crucial relevance of this class will become really evident when I start developing the framework’s model, but for the moment it should be analyzed as a standalone piece of code. Now that the framework has acquired the ability to work with MySQL by means of the “MySQL” class that you saw before, it’s time to list its full source code, so you can see the progress that has been achieved so far. This will be done in the last section of this tutorial, so click on the link that appears below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|