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

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


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

Dev Shed Tutorial Topics: