Home arrow PHP arrow Page 2 - Building a MySQL Abstraction Class with Method Chaining

Building a MySQL abstraction class - PHP

In this fourth part of a 12-part series on method chaining, I start building a basic MySQL abstraction class that implements a few straightforward methods. Of course, the methods can be easily chained to each other, which permits us to build different parts of a SELECT statement through a truly compact and readable API.

TABLE OF CONTENTS:
  1. Building a MySQL Abstraction Class with Method Chaining
  2. Building a MySQL abstraction class
  3. Defining a chainable method to perform SELECT statements
  4. Building the WHERE part of a query
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
November 02, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: