Seeing as I learn faster by doing than just reading, I decided to start playing around with classes in PHP. I’m running PHP4 (as that is what my web host supports), and decided I’d start by re-inventing the wheel and create a class for interfacing with MySQL.
Question 1: As far as I can tell, PHP4 classes are upward-compatible to PHP5, with the caveat that E_STRICT warnings will be generated by attribute declarations since PHP4 does not support the public/private/protected visibility declaration. Is there any other “gotcha” when running a PHP4 class under PHP5 I should be aware of?
Question 2: Below is the current incarnation of my DB class definition. It works as intended, I’m just curious if any OO experts see anything glaringly wrong in its design with regard to good OO practices.
[code=php]
<?php
######################################################################
# CLASS: DB
# PURPOSE: Provide common database functionalities
# METHODS:
# DB: (constructor) initiate connection to MySQL and select database
# querySelect: process a SELECT query and return results in array
# queryOther: process queries which are not SELECTs (e.g.: INSERT,
# DELETE, UPDATE)
#
# HISTORY:
# Created: 2005/12/04 by Charles Reace
######################################################################
class DB {
### ATTRIBUTES ###
# modify dataFile to specify CSV file that defines rest of fields:
var $connectData = array(‘dataFile’ => ‘c:db.txt’,
‘host’ => ”,
‘user’ => ”,
‘password’ => ”,
‘database’ => ”);
var $connection = “”; # db connection handle from mysql_connect()
### METHODS ###
######################################################################
# bool DB()
# constructor
#
# Establishes connection with MySQL and selects database as per
# parameters in $this->connectData, which has most fields populated
# from CSV file $this->connectData[‘dataFile’]
######################################################################
function DB()
{
$handle = @fopen($this->connectData[‘dataFile’], ‘r’);
if($handle === FALSE)
{
die(“Unable to open dataFile.”);
}
while( ($line = fgetcsv($handle, 1000)) !== FALSE )
{
if(count($line) == 2)
{
$this->connectData[$line[0]] = $line[1];
}
}
fclose($handle);
foreach($this->connectData as $value)
{
if(empty($value))
{
die(“Not all connection data fields populated”);
}
}
$this->connection = @mysql_connect($this->connectData[‘host’],
$this->connectData[‘user’],
$this->connectData[‘password’]);
if(!$this->connection)
{
die(“Unable to connect to database server”);
}
$result = mysql_select_db($this->connectData[‘database’]);
if(!$result)
{
die(“Unable to select database – ” . mysql_error());
}
return(TRUE);
} # end constructor method
######################################################################
# array querySelect(str queryString [, bool debug])
#
# PURPOSE: process a SELECT query
# RETURNS: array of query results as array[rowNbr][fieldname],
# FALSE if query failed, or 0 if now rows returned
######################################################################
function querySelect($queryString, $debug = FALSE)
{
$result = @mysql_query($queryString, $this->connection);
if(!$result) # query failed
{
if($debug) # die only if debug on
{
die(“querySelect(): Query failed – $queryString – ” . mysql_error());
}
return(FALSE); # false = mysql error
}
if(mysql_num_rows($result) == 0) # query OK, but now rows returned
{
return(0); # 0 = no rows found that satisfied query
}
$count = 0;
# if we got here, we got query results, so put them in array:
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $value)
{
$output[$count][$key] = $value;
}
$count++;
}
return($output);
} # end method querySelect()
######################################################################
# int queryOther(str queryString [, bool debug])
#
# PURPOSE: process a query other than a SELECT
# RETURNS: num of rows affected, or FALSE if failed
######################################################################
function queryOther($queryString, $debug = FALSE)
{
$result = @mysql_query($queryString, $this->connection);
if(!$result) # query failed
{
if($debug) # die only if debug on
{
die(“querySelect(): Query failed – $queryString – ” . mysql_error());
}
return(FALSE); # false = mysql error
}
# query OK, return affected rows
return(mysql_affected_rows($this->connection));
} # end method queryOther()
} # end class DB
?>
Well, if you’ve read this far, my sincerest thanks!
?