Home arrow PHP arrow Page 3 - Completing the MySQL Class with Method Chaining

Dynamically creating the LIKE part of a query - PHP

Among the numerous features provided by PHP 5, there’s one that many developers find appealing. It permits the building of compact and modular programming interfaces. Yes, as you may have guessed, in this specific case I’m talking about method chaining, a programming approach can be easily mastered by those with an average background in the object-oriented paradigm. This is the sixth part of a 12-part series on method chaining.

TABLE OF CONTENTS:
  1. Completing the MySQL Class with Method Chaining
  2. Review: creating ORDER BY SQL clauses with chainable methods
  3. Dynamically creating the LIKE part of a query
  4. Adding a chainable factory method to the MySQL class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 09, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I anticipated in the section that you just read, the next step that I'm going to take involves defining yet another chainable method within the MySQL abstraction class. It will be tasked with building the LIKE clause of a SELECT statement.

The implementation of this method will be a bit more complex than the others, but don't worry, it'll still be quite simple to grasp. So let's see how this particular method looks. Here it is:

// create the LIKE part of the query

// chainable

public function like($match = '', $wildcard = 'both')

{

if ($match != '')

{

if ($wildcard === 'both')

{

$this->query .= ' LIKE ' . ''%' . $match . '%'';

}

else if ($wildcard === 'pre')

{

$this->query .= ' LIKE ' . ''%' . $match . ''';

}

else if ($wildcard === 'post')

{

$this->query .= ' LIKE ' . ''' . $match . '%'';

}

}

return $this;

}

As depicted above, the "like()" method builds the LIKE clause of a query, and it takes two arguments: the string to be matched, and one that is used internally to specify the position of the wildcard within the clause. Not too hard to understand, right?

Now, after implementing this new method, the full source code of the "MySQL" abstraction class will look 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());

}

}

 

// create the SELECT part of the query

// chainable

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

{

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

return $this;

}

 

// create the WHERE part of the query

// chainable

public function where($where)

{

$this->query .= ' WHERE ' . $where;

return $this;

}

 

// create the ORDER BY part of the query

// chainable

public function orderby($order = 'id ASC')

{

 $this->query .= ' ORDER BY ' . $order;

 return $this;

}

 

// create the LIKE part of the query

// chainable

public function like($match = '', $wildcard = 'both')

{

if ($match != '')

{

if ($wildcard === 'both')

{

$this->query .= ' LIKE ' . ''%' . $match . '%'';

}

else if ($wildcard === 'pre')

{

$this->query .= ' LIKE ' . ''%' . $match . ''';

}

else if ($wildcard === 'post')

{

$this->query .= ' LIKE ' . ''' . $match . '%'';

}

}

return $this;

}

 

// fetch result set

public function fetch()

{

if (FALSE === ($this->result = mysqli_query($this->link, $this->query)))

{

throw new Exception('Error performing query ' . $this->query);

}

return $this->result;

}

}

And now that you have at your disposal the complete signature of the MySQL abstraction class after implementing the "like()" method, here's an example of how to use it:

try

{

$db = new MySQL('host', 'user', 'password', 'database');

// fetch result set by chaining methods of MySQL class

$result = $db->select('properties')->where('title')->like('c', 'post')->fetch();

while ($row = mysqli_fetch_object($result)) {

echo $row->title . $row->description. '<br />';

}

}

catch(Exception $e)

{

echo $e->getMessage();

exit();

}

There you have it. Now, the "MySQL" class is capable of performing conditional SELECT queries by chaining three different, highly modular methods. Of course, it's also feasible to create a few others, but I'll leave this task as homework for you.

However, there's a task that remains undone that I mentioned in the introduction. It will definitely be very useful to create a factory method within the MySQL abstraction class that returns singletons and that can be chained to other methods.

That will be the conclusion for this article. The details of this process will be discussed in the section to come. So, 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: