The first step that I’m going to take in building a simple MySQL abstraction class whose methods will be entirely chainable will consist of defining and initializing its properties, as well as implementing its constructor. Having said that, the starting definition of this class, which I decided to call simply “MySQL,” looks like this: class MySQL { private $host = ''; private $user = ''; private $password = ''; private $database = ''; private $query = ''; private $result = NULL; private $link = NULL; private static $instance = NULL;
// constructor public function __construct($host, $user, $password, $database) { if (FALSE === ($this->link = mysqli_connect($host, $user, $password, $database))) { throw new Exception('Error : ' . mysqli_connect_error()); } } } Well, if you pay close attention to the signature of the above “MySQL” class, you’ll surely notice that it’s extremely easy to grasp. For the time being it only defines the constructor, which in this case is tasked with connecting to the database server and throwing an exception if this process fails for whatever reason. In addition, it’s fair to point out that I decided to use the “mysqli” PHP extension to perform all of the tasks related to MySQL, but you can use the old MySQL extension to do the same things if you feel more comfortable working with it. So far, everything looks pretty good, right? At this stage, I built a PHP 5 class that’s only capable of establishing a connection to the MySQL server. So, the question now is, where do chainable methods come in? Well, don’t worry; in the following segment I’m going to add another method to the previous “MySQL” class, which will be tasked with building the SELECT part of a SQL query -- and will be chainable as well. To see how this method will be implemented, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|