Ciao.
In OOP si chiama composition.
Eccoti un esempio
(è un abbozzo non è completa):
Codice PHP:<?php
class AbstractDb
{
//////
// Legend :
// private $__varName
// public $varName
/////
var $__host;
var $__user;
var $__password;
var $__database;
var $conId; // connection identifier
var $result;
function AbstractDb($options=array())
{
if(count($options)>0)
{
foreach($options as $parameter => $value)
{
(!empty($value))?$this->{$parameter}=$value:$this->isError('Invalid parameter '.$parameter);
}
$this->conId = NULL;
$this->result = NULL;
$this->__connectDB();
}
else
{
$this->isError('No connection parameters were provided');
}
}
function __connectDB()
{
if(!$this->conId=mysql_connect($this->__host,$this->__user,$this->__password))
{
$this->isError('Error connecting to the server');
}
if(!mysql_select_db($this->__database,$this->conId))
{
$this->isError('Error selecting database'.' '.$this->__database);
}
}
function safeQuery($value)
{
if(!is_resource($this->conId))
{
$this->__connectDB();
}
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
if (!is_numeric($value))
{
$value = "'".mysql_real_escape_string($value)."'";
}
return $value;
}
function performQuery($query)
{
if(!is_resource($this->conId))
{
$this->__connectDB();
}
if(!$this->result=mysql_query($query,$this->conId))
{
$this->isError('Error performing query '.$query);
}
// return new Result object
return new ResultDb($this);
}
function getInsertId()
{
if(!$id=mysql_insert_id($this->conId))
{
$this->isError('Error getting ID');
}
return $id;
}
function isError($errorMsg)
{
$error = mysql_errno() . ": " . mysql_error() ;
exit($errorMsg.' '.$error);
}
function closeConId()
{
mysql_close($this->conId);
}
}//END AbstractDb
class ResultDb
{
var $mysql; // instance of AbstractDb object
function ResultDb(&$mysql)
{
$this->mysql=&$mysql;
}
function fetchObject()
{
return mysql_fetch_object($this->mysql->result);
}
function fetchRowAss()
{
return mysql_fetch_array($this->mysql->result,MYSQL_ASSOC);
}
function fetchRowNum()
{
return mysql_fetch_array($this->mysql->result,MYSQL_NUM);
}
function getNumRows()
{
return mysql_num_rows($this->mysql->result);
}
}//END ResultDb
?>![]()


Rispondi quotando