Generic Class Wrapper
The objective of this wrapper is to provide a streamlined interface to data stored in a database.
Notes
- Object properties are stored in an array. This makes managing them must easier. Upon instantiation, the array is loaded with empty strings.
- Magic methods are used to set and return properties.
- The properties have a one-to-one, exact mapping to the underlying database.
- A generic object exists beneath the specific object. It accepts a table name, identifier column name, and an array of values.
item.class.php
<?php
/*mysql> show full columns from items;
+---------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+---------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| item_id | int(10) unsigned | NULL | | PRI | NULL | auto_increment | select,insert,update,references | |
| name | varchar(64) | utf8_unicode_ci | | | | | select,insert,update,references | |
| url | varchar(128) | utf8_unicode_ci | | MUL | | | select,insert,update,references | |
| text | text | utf8_unicode_ci | | | | | select,insert,update,references | |
+---------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
4 rows in set (0.02 sec)
*/
require_once 'obj.class.php';
Class Item Extends Obj
{
private $aData;
public function __construct()
{
$this->aData=array_fill_keys(array('item_id','name','url','text'),'');
parent::__construct();
}
public function __set($name, $value)
{
$this->aData[$name] = $value;
}
public function __get($name)
{
if (array_key_exists($name, $this->aData))
return $this->aData[$name];
}
public function load($id)
{
$this->aData=parent::load('items','item_id',$id);
}
public function save()
{
return parent::save('items','item_id',$this->aData);
}
}
?>
obj.class.php
<?php
require_once 'db.class.php';
Class Obj
{
private $db;
public function __construct()
{
$this->db=Database::singleton();
}
protected function load($sTable,$sIdName,$sId)
{
$aResult=false;
$sQuery='SELECT * FROM `'.$sTable.'` WHERE `'.$sIdName.'`=\''.$this->db->SQLescape($sId).'\'';
$this->db->query($sQuery);
$aResult=$this->db->fetch_assoc();
$this->db->free_result();
return $aResult;
}
protected function save($sTable,$sIdName,$aArgs)
{
$sWhere=' WHERE `'.$sIdName.'`=\''.$this->db->SQLescape($aArgs[$sIdName]).'\'';
unset($aArgs[$sIdName]);
$sSet=$this->db->sPair($aArgs);
$sQuery='SELECT * FROM `'.$sTable.'`'.$sWhere;
$this->db->query($sQuery);
if ($this->db->num_rows()>0)
$sQuery='UPDATE `'.$sTable.'` SET '.$sSet.$sWhere;
else
$sQuery='INSERT INTO `'.$sTable.'` SET '.$sSet;
$this->db->free_result();
return $this->db->query($sQuery);
}
protected function remove($sTable,$sIdName,$sId)
{
$sQuery='DELETE FROM `'.$sTable.'` WHERE `'.$sIdName.'`=\''.$this->db->SQLescape($sId).'\'';
return $this->db->query($sQuery);
}
public function __destruct()
{
}
}
?>
A search function will be added.
db.class.php
<?php
Class Database
{
private static $instance;
private $rLink;
private $db;
private $ini;
private $rResult;
public function __construct()
{
$this->ini = parse_ini_file('config.ini.php','true');
$this->rLink = mysql_connect
($this->ini['db']['host'],
$this->ini['db']['user'],
$this->ini['db']['password']);
if ($this->rLink === false)
trigger_error(mysql_error());
$this->db = mysql_select_db ($this->ini['db']['database'],$this->rLink);
if ($this->db === false)
trigger_error(mysql_error());
return true;
}
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
public function SQLescape($s)
{
return mysql_real_escape_string($s,$this->rLink);
}
public function host_information()
{
return mysql_get_host_info($this->rLink);
}
public function query($sQuery)
{
$this->rResult=mysql_query($sQuery,$this->rLink);
if ($this->rResult === false)
trigger_error(mysql_error());
return $this->rResult;
}
public function fetch_assoc()
{
return mysql_fetch_assoc($this->rResult);
}
public function sPair($aPair)
{
$sReturn='';
foreach ($aPair as $k => $v)
$sReturn .= "`$k`='".$this->SQLescape($v).'\', ';
$sReturn=substr($sReturn,0,-2);
return $sReturn;
}
public function sWhere($aWhere)
{
$sReturn='';
foreach ($aWhere as $k => $v)
$sReturn .= " `$k`='".$this->SQLescape($v).'\' AND ';
return substr($sReturn,0,-4);
}
public function sRegExp($aRegExp)
{
$sReturn='';
foreach ($aRegExp as $k => $v)
$sReturn .= " `$k` REGEXP '".$this->SQLescape($v).'\' AND ';
return substr($sReturn,0,-4);
}
public function num_rows()
{
return mysql_num_rows($this->rResult);
}
public function free_result()
{
mysql_free_result($this->rResult);
}
public function __destruct()
{
if (isset($this->rLink))
if ($this->rLink !== false)
mysql_close($this->rLink);
}
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
?>
Print article | This entry was posted by elvis on 05/02/09 at 12:19:48 am . Follow any responses to this post through RSS 2.0. |