View Single Post
Old 10-11-2007, 12:11 PM   #8 (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

Nice start. Here are a few poiners.

1) Change the connect() method to accept arguments for setting up the database connection. THe connection details seem to be hard coded currently. Chanign this will allow you to easily reuse this object in future projects.

2) If you add some "wrapper" methods to your Database class for retrieving things such as a row, column or array of results (as I mentioned in my orignal post) then you could make your Member class much more flexibile and reduce the amount of repeated code.

Lets say you do add the wrapper functions (something like this):

PHP Code:
public function query($szSql)
{
    
$pResult mysql_query($szSql);

    ... 
convert result to array ...

    return 
$aResult;
}

public function 
queryRow($szSql
{
    
$aResult $this->query($szSql);
    return 
$aResult[0];
}

public function 
queryColumn($szSql)
{
    
$aRow $this->queryRow($szSql);
    return 
$aRow[0];

You can then perform the same query as before using:

PHP Code:
$pDatabase->queryRow('SELECT id FROM members WHERE id = 1'); 
You could wrap that in another "wrapper" function to give it a more user friendly interface, such as:

PHP Code:
function getById($iId)
{
    
$iId = (int) $iId;
    return 
$this->pDatabase->queryRow("SELECT id FROM members WHERE id = $iId");

Then to achieve the same thing, we simply use

PHP Code:
$aMember $pMember->findById(1); 
You could actually create another class for handling these database "interfaces". This other class would be similar to that of a Model from the MVC pattern, including helper functions for achieving things such as:

PHP Code:
// Fetch the Id from the member table with Id 1
$pMembers->setId(1);
$aMember $pMembers->fetch('id'); 
and

PHP Code:
// Fetch all members from the members table
$aMembers $pMembers->fetchAll(); 
I better stop there before I completely overwhelm you. Lemmie know if any of that makes any sense :)
Karl is offline  
Reply With Quote