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
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: