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

Defining a chainable method to perform SELECT statements - 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

In the previous section, I defined and implemented the constructor of the MySQL abstraction class, a process that should be pretty understandable to you. Now, it’s time to extend the class’s current functionality by adding to it a brand new chainable method for creating the SELECT portion of a query.

The implementation of this method, called “select(),” is show below. Take a look at it:

// create the SELECT part of the query

// chainable

public function select($table, $fields = '*')

{

$this->query = ' SELECT ' . $fields . ' FROM ' . $table;

return $this;

}

See how simple it is to define a method that builds the SELECT part of a SQL query? I guess you do. In this specific case, the method not only will create that portion of the query according to the specified fields, but since it returns an instance of the MySQL class, it can be linked easily to others as well.

But I’m getting ahead of myself; please pay attention to the following code sample, which shows the signature of the sample “MySQL” class, this time including the “select()” method:

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());

}

}

 

// create the SELECT part of the query

// chainable

public function select($table, $fields = '*')

{

$this->query = ' SELECT ' . $fields . ' FROM ' . $table;

return $this;

}

}

The above “MySQL” class is beginning to take shape, even though in its current state it is still quite basic. However, this is about to change; in the next section I’m going to add to it yet another chainable method, which will be responsible for building the WHERE modifier of a SQL query.

To learn how this will be accomplished, please click on the link below and read the next few lines.



 
 
>>> 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 6 - Follow our Sitemap

Dev Shed Tutorial Topics: