View Single Post
Old 10-10-2007, 02:48 PM   #3 (permalink)
Karl
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Well the first thing before starting any project is to build a basic design, take a look at your system as a whole and decide what objects make up your system.

One class you will probably want to start with is a Database class. This class would provide methods that allow you to encapsulate and simplify the various database tasks that you need to perform. For example, I use something similar to:

PHP Code:
class Database
{
  private 
$m_pConn;
  ...

  public 
funciton __construct() { ... }
  public 
funciton connect() { ... }
  public 
funciton query() { ... }
  public 
funciton queryColumn() { ... }
  public 
funciton queryRow() { ... }
  ...

Inside the __constructor you would create a new database connection by calling the connect() function.

The connect function would create you a new database connection and store it in $m_pConn.

The query, queryRow and queryColumn methods provide simpler interfaces for performing common "SELECT" operations, for example, we could fetch a single member from the database using the queryRow method:

PHP Code:
$pDatabase = new Database();
$aMember $pDatabase->queryRow('SELECT username, email FROM members WHERE id = 4'); 
Don't get me wrong, this isn't going to be something you achieve in a night. It takes time to build efficient, reusable objects, but when you build them right, you can pop them into projects and reuse your code time after time.
Karl is offline  
Reply With Quote