Home arrow PHP arrow Page 2 - Handling MySQL Data Sets with Static Variables in PHP 5

Review: using a static property within a sample class - PHP

In this second part of a four-part series, I finish building a sample MySQL driver by adding to it a whole new class responsible for iterating over database result sets through a friendly and intuitive API. This functional example shows that the use of a simple static property makes it easier to implement the Singleton pattern, which is convenient when developing a class that abstracts the access to a database server, be it MySQL or a different one.

TABLE OF CONTENTS:
  1. Handling MySQL Data Sets with Static Variables in PHP 5
  2. Review: using a static property within a sample class
  3. Building a MySQLi_Result wrapper class
  4. Putting the MySQL driver to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 28, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before we start building the second component of the MySQL driver mentioned in the introduction, it’d be really helpful to reintroduce the definition of the first class that comprises this driver. Among other tasks, it connects to the database server, runs SQL queries and so forth via a Singleton object.

The full source code of this class is as follows:

class MySQLiWrapper extends MySQLi

{

 

private static $_instance = NULL;

private $_config = array();

 

// return Singleton instance of MySQL class

public static function getInstance(array $config = array())

{

if (self::$_instance === NULL)

{

self::$_instance = new self($config);

}

return self::$_instance;

}

 

// private constructor

private function __construct(array $config)

{

if (count($config) < 4)

{

throw new Exception('Invalid number of connection parameters');

}

$this->_config = $config;

 

}

 

// prevent cloning class instance

private function __clone(){}

 

// establish a connection to MySQL

public function connect()

{

list($host, $user, $password, $database) = $this->_config;

parent::__construct($host, $user, $password, $database);

if ($this->connect_error)

{

throw new Exception('Error connecting to MySQL : ' . $this->connect_errno . ' ' . $this->connect_error);

}

unset($host, $password, $database);

}

// perform query

public function runQuery($query)

{

if (is_string($query) AND !empty($query))

{

// lazy connect to MySQL

$this->connect();

// run the specified query

if ((!$this->real_query($query)))

{

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

}

return new MySQLi_ResultWrapper($this);

}

}

 

// get insertion ID

public function getInsertID()

{

return $this->insert_id;

}

 

// close database connection

public function __destruct()

{

$this->close();

}

}

As I said before, the above “MySQLiWrapper” class performs a few simple tasks, such as opening and closing a connection to MySQL, executing hard-coded SQL statements and so forth. While the respective implementations of the methods that carry out all these operations do deserve further analysis, the most important detail to stress here is the use of the static $_instance property within the “getInstance()” method. It returns to client code a Singleton instance of the class.

This method itself demonstrates a concrete use of a static variable that not only improves the behavior of its containing class, but also boots the performance of the application using the class in question.

And now that you’ve recalled the inner workings of the previous “MySQLiWrapper” driver, it’s time to start defining the class that iterates over result sets, since an object spawned from this class is created within the corresponding “query()” method.

As you’ll see in a few moments, this whole new class will be an implementer of the Iterator and Countable PHP 5 native interfaces. Its definition will be shown in the section to come. Therefore, to get there click on the link that appears below and keep reading.



 
 
>>> 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: